New string stuff in Whidbey

The .net framework 2.0 comes with quite a few exciting new features. One of the less visible, yet very useful enhancement is String.Split's ability to split by a group of strings; in 1.1 you only could split by single characters. In 2.0, you can say "a=b and c=d".Split("and").

You can also give Split a new StringSplitOptions enum parameter where you can specify options related to splitting. The only option existing at the moment (2.0 beta 1) is RemoveEmptyEntries. Naturally it just removes empty elements which occur if separators immediately follow each other. I'm pretty surprised the framework team didn't bother with "CaseInsensitive" and "TrimResult" options, too: CaseInsensitivity exists in many string functions now (with 2.0 the support got added to StartsWith and EndsWith, too), and trimming all the split results is often useful when splitting natural text by words (which tend to have spacing around them).

As it is, case insensitive string splits must still be done using Regex.Split, which of course allows you to make the Regex case insensitive – and it's easy to make the Regex ignore the spacing as well. The following code sample is a good illustration:

string text = "a=b OR c=d and e=f";
text.Split(new string[] { "and", "or" });
// produces "a=b OR c=d ", " e=f" (note the whitespace)
Regex.Split(text, "\\s*(?:and|or)\\s*", RegexOptions.IgnoreCase);
// produces "a=b", "c=d", "e=f"

Regular expressions save the world again, but a properly equipped Split would be so much easier to use…

For a listing of other new stuff in the framework class library 2.0, see the article on BCLTeam blog.

August 1, 2004 · Jouni Heikniemi · One Comment
Posted in: .NET

One Response

  1. Elise Naff - January 12, 2021

    I like the posts on this site… I’ll be coming back for sure.

Leave a Reply