.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.
Supported by the .NET Compact Framework.
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.
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.