Friday, July 30, 2010

Useful Extension Method: SplitNoEmpty

I ran into a situation where there were some extra spaces in some splits I was doing. I was unaware there was an option StringSplitOptions.RemoveEmptyEntries, which would fix my problem. But to cut down on text in the code since I have splits all over the place, I created the following extension class.
public static class StringSplitExtension
{
public static string[] SplitNoEmpty(this string val, string delim)
{
return val.Split ( delim.ToCharArray (), StringSplitOptions.RemoveEmptyEntries );
}
}

So now, I just change my splits from-

mystring.split(' ');
to-
mystring.SplitNoEmpty(" ");




No comments: