ASP.Net C# Split String Function

Updated on 17 May 2012,
Published on 01 Jul 2008

ASP.Net C# Split string function provides the functionality to split the string into a string array by specifying its delimiters. C# split string function splits the string into array collection according to the number of separators passed to the split function. ASP.Net C# split string function removes the delimiters from the string and stores each part separated at consecutive indexes of array object. In ASP.Net 2.0 C# split string function has 6 overloads as follows:

// 1. 
public string[] Split(params char[] separator);

// 2. 
public string[] Split(char[] separator, int count);

// 3. 
public string[] Split(char[] separator, StringSplitOptions options);

// 4. 
public string[] Split(string[] separator, StringSplitOptions options);

// 5. 
public string[] Split(char[] separator, int count, StringSplitOptions options);

// 6. 
public string[] Split(string[] separator, int count, StringSplitOptions options);

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

You can pass the char array separator or string array separator to the split string function of C#. Other two parameters of overloaded split functions are int count and StringSplitOptions options. Integer type count parameter accepts integer value that specifies the number of substrings to be returned by the split action. If count value is passed then it returns the specified number of substrings by removing and splitting from the delimiter specified and leaves the remaining string unchanged. StringSplitOptions is an enumerator type that accepts two types of enumerated options as "None" or "RemoveEmptyEntries". It removes the empty string from the generated string array.

Example of ASP.Net C# Split String Function

string strData = "a,b,c,d,e,f,g,h,i,j";
char[] separator = new char[] { ',' };

string[] strSplitArr = strData.Split(separator);

foreach (string arrStr in strSplitArr)
    Response.Write(arrStr + " ");

Above example code of C# split function will split string from "," separator specified in the char array separator and generates a string array. C# foreach loop through the array and reads the value at each index of the array.


a b c d e f g h i j

You can use C# split function overloads as in the following examples:

Example of ASP.Net C# Split string function with string array separator

string strData = "a,b,c,d,e,f,g,h,i, ,j";

string[] separator = new string[] { "," };

string[] strSplitArr = strData.Split( separator, StringSplitOptions.RemoveEmptyEntries);

foreach (string arrStr in strSplitArr)
   Response.Write(arrStr + " ");


a b c d e f g h i j

Note: StringSplitOptions.RemoveEmptyEntries removed the empty string value from the array elements.

Example of ASP.Net C# Split string function with char array and count parameter

string strData = "a,b,c,d,e,f,g,h,i,j";

char[] separator = new char[] { ',' };

string[] strSplitArr = strData.Split(separator,3);

foreach (string arrStr in strSplitArr)
   Response.Write(arrStr + "<br />");


Above example returned three substrings as specified in the count parameter of C# split string function. It returned first and second substring separated from the specified delimiter and third substring unchanged.


Learn with the help of C# Split function sample from the following link:

String Split Function

Continue to next tutorial: ASP.Net C# Remove Characters String Function to learn how to delete characters from the specified string.

30 Responses to "ASP.Net C# Split String Function"
It is nice
its very helpful
It's great, in the whole description above mentioned
the split function... It's very assist for me.
Thnk's f or describing split function.
Thanks! It helped me.
It's very nice and usefull..
I want to print (item1,item2) in the it do it?
Hi Priya

To display comma separated values inside the Label control you can try the following code:

string strData = "a,b,c,d,e,f,g,h,i,j";

char[] separator = new char[] { ',' };

string[] strSplitArr = strData.Split(separator);

foreach (string arrStr in strSplitArr)
Label1.Text += arrStr + "


In the above code snippet Label1.Text +=
will concatenate each item while executing the foreach loop.

Hope this will help you
About damn time a webpage made sense about this issue. Thanks!
thanks it was good for me
Good,It helped me
thanks for my proble solving
It's is a fantastic one i like very much.
I want to display colon seperated values into double format like 12Hours:23minitues:56secods is, double var1=12 double var2=23 double var3=56 how can i do this?
Eddy Tan
Thank for sharing, very helpful for my project :D
its nice so simple i like thank u.
Excellent article. Thanks.
Amit kalke
your code is zakkkkaaaass !!!
string myString = "Hola_Mundo";
string separator= "_";

string result = MyString.Split(separator)[0];

string result = MyString.Split('_')[1];
Its a really nice explaination given by u, It hepled me to solve the problem.
i want to use 90 radiobuttons can anyone tell me how to put up radiobuttons in loop eg radiobutton+"i"+.checked=true wasnt working any idea?
public static DataTable findAdmin(string Username1, string Password1) { DataSet ds = new DataSet(); OleDbConnection oConn = new OleDbConnection(ConfigurationManager.ConnectionStrings["TESTconnectionstring"].ToString()); OleDbDataAdapter oDa = new OleDbDataAdapter(new OleDbCommand()); oDa.SelectCommand.CommandText = "Select * From tblAdminlogin where (Username1=@Username1) and (Password1=@Password1)"; oDa.SelectCommand.CommandType = CommandType.Text; oDa.SelectCommand.Connection = oConn; try { oDa.SelectCommand.Parameters.AddWithValue("@Username1", Username1); oDa.SelectCommand.Parameters.AddWithValue("@Password1", Password1); oConn.Open(); oDa.Fill(ds, "tblAdminlogin"); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (oConn.State == ConnectionState.Open) { oConn.Close(); } } return ds.Tables[0]; } Insert into tblSubject (Subject, DateCreated, User_Made) values (@Subject, DateCreated, @User_Made) Update tblQuestions SET Question=@Question, Answer=@Answer, DateTimeCreated=@DateTimeCreated where (QuestionID=@QuestionID) Delete tblOptions.* from tblOptions where BankID=@BankID
Can anyone let me know about the following scenario: ANYSTRING_ON_LEFT#ANYSTRING_ON_RIGHT If I have any set of string on the left of # and any set of string on the right of #, then, the left part should be saved in one string var and the right string should be stored in another string var.
Great article. I just wanted to let others know that I have written an extension method similar to the stock string.Split method but it allows you to specify the return type of the collection that is generated after the splitting is performed. I have found it useful in string manipulation. The code can be found at:
jai mata di
arter split it convert

i need to split my string from the first separator of my string because in my string there are more then one same separator. Example: Hi:good:morning:india then i want split my string from first : symbol and display like Hi good:morning:india
@Ashish Hi there you can use the following code to split your string into your specified way: // your original string here string myText = "Hi:good:morning:india"; // split the string into array string[] splitMyText = myText.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries); // use System.Text to build the new string StringBuilder myMessage = new StringBuilder(); // get the first item from array to display it separately myMessage.AppendFormat("{0}
", splitMyText[0]); // build the string from second to last item of array for (int index = 1; index < splitMyText.Length; index++) { myMessage.AppendFormat("{0} ", splitMyText[index]); } // display the string on page Response.Write(myMessage.ToString()); Hope this will work for you.
prince demmy
its helpful. thanks
Alfred George
thanx, it was helpful. my problem is solved...............
Hi, i have a parameter like this: header=title|||value|||END|||&detail=abc|||1|||END|||def|||2|||END|||ghi|||3|||END||| i want to display the value like this: title value abc 1 def 2 ghi 3
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email