How would you split with C-Sharp

How would you split a string with comma separated values and remove any blank records?

While the solution may seem obvious to most folks, it is not always obvious to everyone. I have been asking perspective candidates this question, but there is one common answer that I get.

“Create a loop with two array variables. Loop through the comma separated string and do substring to extract each value while keeping the current index to use as your next starting point. Then create another loop to loop through your new array of strings to add the strings with length greater than zero to a new array… then return your newest array of strings.”

That seems like a lot of loops, a lot of string arrays and a lot of thinking.

Whenever I suggest using the String object’s split command using the RemoveEmptyEntries; I get told “Oh, what I told you is probably what DotNet is doing under the hood.

My god, that seems like a lot of work even for DotNet. I urge everyone to look at the functions available before undertaking such routines. Can you imagine the code that would be produced by a developer who is always writing code like described above? If you are unsure, use google to find your answer or even better yet use MSDN. Do not assume you know everything, because you are not expected to. There are resources around… Internet is a developer’s best friend.

Source for String Split Options: http://msdn.microsoft.com/en-us/library/ms131448.aspx

Sample Google Query used to find the answers:

Posted in interview Tagged with: