.Net 2.0 smart String Split() functionality

There is a new version of the String Split() function, a split with the option to remove empty entries is the resulting string array. This new function solves your problems with lazy encoded multi-value strings. I know, this is one of the zillion things that were improved in .Net 2.0, and a lot of people probably know this one for ages. For me it was completely new however, and if it is new for me, there must be someone out there who didn’t know this as well;-)

The case:

Often you create a multi-value string separated with a special character at one end of your application, and you retrieve them at the other end of you application by splitting the string on the special character.

Example: at one side you have list of users that you encode as a string;

List of users: "serge", "dinah", "scott", "dean". Encode as: "serge;dinah;scott;dean"

There are two special cases you have to take into account, either on constructing the multi-value string, or on splitting the multi-value string into its values:

  • Empty entries
  • A trailing special character

Assume you have the following array of strings: "one","", "three","","five". You can either encode it the lazy and simple way, or the complex way. In the simple way you leave it up to the receiving side to skip empty entries and the trailing special character. In the complex way you deliver clean data on the producing side by skipping empty entries and not writing the special character after the last entry.

Producing Lazy and Simple:

Resulting string: "one;;three;;five;"

string result;
foreach (string s in stringArray)
{
    result += s + ";";
}

UPDATE: As Danny de Haas pointed out to me this can ofcourse be done even lazier:

string result = String.Join(";", stringArray);


Producing Complex:

Resulting string: "one;three;five"

string result;
for (int i=0; i<stringArray.Length; i++)
{
    if (!String.IsNullOrEmpty(stringArray[i]))
    {
        result += stringArray[i];
        if (i != stringArray.Length – 1)
        {
             result += ";";
        }
    }
}

The above code is maybe not the smartest code, but you get the drift.

Receiving:

If you receive a string and you want to take into account that the string could be produced in the lazy and simple way you had to do extra processing on the array you get from result.Split(‘;’) to remove empty entries resulting from encoded empty entries or the trailing special character when working with the .Net 1.1 framework. The .Net 2.0 framework now provides an overload of the String Split() function that is smart enough to handle this case:

string[] resultArray = result.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);

This call takes care of the empty entries and the empty entry at the end produced due to a trailing special character.

See http://msdn.microsoft.com/en-us/library/system.string.split.aspx for more information in the Microsoft documentation on all overloads of the split function, and http://msdn.microsoft.com/en-us/library/system.stringsplitoptions.aspx for more information on the StringSplitOptions.

Name Description
String.Split (Char[]) Returns a String array containing the substrings in this instance that are delimited by elements of a specified Char array.

Supported by the .NET Compact Framework.

String.Split (Char[], Int32) Returns a String array containing the substrings in this instance that are delimited by elements of a specified Char array. A parameter specifies the maximum number of substrings to return.
String.Split (Char[], StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified Char array. A parameter specifies whether to return empty array elements.
String.Split (String[], StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified String array. A parameter specifies whether to return empty array elements.
String.Split (Char[], Int32, StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified Char array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.
String.Split (String[], Int32, StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified String array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.

 

14 Comments

  • Good article; this is what I was looking for.

    thanks, Taylor

  • Thanks,

    The lazy and even lazier parts don't make a lot of sense. But the rest is very useful.

    I had the situation that when I used for example:
    string[] lists = myValue.Split(' ');

    then the first index of the lists array is alway "" which simply retarded. I use this instead:
    string[] lists = myValue.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);


  • Saved as a favorite, I like your website!


  • Can I just say what a comfort to discover someone that really understands what they are talking about over the internet. You certainly realize how to bring an issue to light and make it important. More people have to look at this and understand this side of your story. It's surprising you aren't more popular because you most certainly have the gift.


  • Having read this I believed it was very informative. I appreciate you taking the time and energy to put this informative article together. I once again find myself personally spending a significant amount of time both reading and leaving comments. But so what, it was still worthwhile!


  • Great blog you've got here.. It’s difficult to find excellent writing like yours these days. I seriously appreciate individuals like you! Take care!!


  • Having read this I believed it was extremely informative. I appreciate you taking the time and energy to put this content together. I once again find myself personally spending way too much time both reading and commenting. But so what, it was still worthwhile!


  • The next time I read a blog, Hopefully it does not fail me as much as this particular one. I mean, Yes, it was my choice to read, but I really believed you would probably have something helpful to say. All I hear is a bunch of complaining about something that you could fix if you were not too busy searching for attention.


  • Hello! I could have sworn I’ve visited this blog before but after browsing through a few of the articles I realized it’s new to me. Nonetheless, I’m definitely happy I discovered it and I’ll be book-marking it and checking back often!


  • Greetings! Very useful advice in this particular post! It's the little changes that make the most significant changes. Thanks for sharing!


  • Greetings! Very helpful advice within this article! It is the little changes which will make the most important changes. Many thanks for sharing!


  • Greetings! Very helpful advice in this particular article! It's the little changes which will make the biggest changes. Thanks a lot for sharing!


  • When I originally commented I appear to have clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I get 4 emails with the same comment. Is there a way you are able to remove me from that service? Appreciate it!


  • This blog was... how do you say it? Relevant!! Finally I have found something that helped me. Kudos!

Comments have been disabled for this content.