ASP.NET String Split on String

ASP.NET
ASP.NET

In both C# and VB.NET the split method can be very useful for manipulating strings. It’s good to note that in addition to the default split functionality based on one character, the split method in both languages includes useful overloads for handling multiple characters.

I have posted further ASP.NET articles that may be of interest. Please take a look and let me know what you think!

The default split function behaviour is to split by one character. But splitting by a single character can be risky business since it is likely that it will at one point exist within part of the string that you don’t want to split.

If you want to reduce the chance for characters in user-entered text accidentally causing a problem with your split function, then it’s best to have your split function base itself on a combination of characters. For example instead of splitting based on a single | character, you might prefer to split based on a combination of |:

Splitting Strings in VB.NET

The problem with splitting based on multiple characters is that VB.NET will split on them, but will only remove the first of the split characters. Thus if you were to use a split function on the character combination |:  in the string: test|:text then you would end up with the colon character preceding each of your resulting values. In the case of our example, this would result in strMyString(1) being :text

Thankfully the default split method has several overloads which let you split properly based on multiple characters.

So while the default split function syntax is:

strMyString.Split("|")

You can use an overload of the function to split by the combination |: as follows:

strMyString.Split(New String(){"|:"}, StringSplitOptions.RemoveEmptyEntries)

Here is a more complete example:

Dim strMyString As String = "test|:text"
Dim sarrMyString As String() = strMyString.Split(New String() {"|:"}, StringSplitOptions.None)
Dim strKey As String = sarrMyString(0)
Dim strValu As String = sarrMyString(1)

Splitting Strings in C#

It’s also worthwhile noting that C# encourages you to use the overloaded method for splitting strings. In C# the default split function only supports a Char input. So to split by a | character you would write:

string[] sarrMyString = strMyString.Split('|');

If you want to split by multiple characters in C# then you will have to use the method overload in the same way as you (optionally) do in VB.NET. So as a simple example in C# of splitting by |: you would write:

string strMyString = "test|:text";
string[] sarrMyString = strMyString.Split(new string[]{"|:"}, StringSplitOptions.None);
string strKey = sarrMyString[0];
string strValu = sarrMyString[1];
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s