IMG-LOGO

How to split string in ASP.Net C#?

andy - 14 Jul, 2013 13295 Views 0 Comment

There are two ways to split string in C# ASP.Net, you can use Regex to split the string if you want to split between words. But if you want to split only one single character, you can use the Split function from String object.

See below example for more details:

//first example split a single character
string sampleString = "1,2,3,4,5";
string[] arrayString = sampleString.Split(',');
foreach (string str in arrayString) {
	Response.Write(str + ", ");
}

//second example split a word
string sampleSentence = "Run and faster and go and higher";
string[] arrayWord = System.Text.RegularExpressions.Regex.Split(sampleSentence, "and");
foreach (string str in arrayWord) {
	Response.Write(str + ", ");
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.