RSS

SubString() vs. Split() in C# & VB

23 Oct

In this post we will know the difference between SubString() and Split() functions.

SubString() … !!

This method retrieves a substring from a string. You can specify the starting index and number of characters in a substring you want to retrieve.

String strTest = “My name is: Ahmed Negm”;
MessageBox.Show(strTest.Substring(12));
// Result will be: “Ahmed Negm”

String strTest = “My name is: Ahmed Negm”;
MessageBox.Show(strTest.Substring(12,5));
// Result will be: “Ahmed”

Split() … !!

You want to split strings on different characters with single character or string delimiters. For example, split a string that contains “;” sequences. Through these examples, we learn ways to use the Split method on the string type in the C# programming language.

String strTest = “AhmedNegm@WindowsLive.com;AhmedNegm@Live.com”;
String[] delimiter = {“;”};
String[] strResult;

strResult = strTest.Split(delimiter, 50, StringSplitOptions.None);

// Result will be:

// strResult[0] = “AhmedNegm@WindowsLive.com”
// strResult[1] = “AhmedNegm@Live.com”

Code Notes:-

    1. We have some of strings like e-mails separated by (;) separator, and we want to split this string like the previous string in the example into array.
    2. Number (50) represents the maximum numbers of splited strings.
    3. You can use any overloads of this method.
 
Leave a comment

Posted by on October 23, 2011 in Visual Basic .NET, Visual C# . NET

 

Tags: , , , , ,

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

 
%d bloggers like this: