Tuesday, September 20, 2016

Did you know about StringSplitOptions?

Today I've learned something old. Yeah, it has been there from .NET 2.0 and probably at one time or another I knew about it, then I just forgot about it. I am talking about the string.Split method overload that accepts a StringSplitOptions enumeration. In fact, it is an enum just in name, because it has only two possible values: None and RemoveEmptyEntries.

I had forgotten that I can eliminate empty entries like that, even if, truth be said, string splitting with a regular expression and maybe then filtering the results with LInQ feels much better, both as readability and control over the result. So, what is your preferred method of splitting, say..., a sentence into words?
text.Split(" \t".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
Regex.Split(text,@"[ \t]+").Where(s=>!String.IsEmptyOrWhiteSpace(s));
? (I realize that in the case I am describing I don't need the LInQ at the end except in fringe cases, but it was an example)