String Split() Method
The following asp.net c# example code demonstrate us how can we split a string by specified delimiter and populate an array with splitted
elements programmatically at run time in an asp.net application. .Net frameworks String Class represent text as a series of
Unicode characters.
String.Split() method return a new string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array. This member is overloaded, those are String.Split(Char[]), Split(Char[], Int32), Split(Char[], StringSplitOptions), Split(Char[], Int32, StringSplitOptions) and Split(String[], Int32, StringSplitOptions).
In this example, we only discuss about String.Split(Char[]) overloaded method. String.Split(Char[]) method return a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array. This method require to pass a parameter named 'separator' which type is System.Char[]. This parameter value represent an array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters or null.
String.Split(Char[]) method return value type is System.String[] which represent a string array whose elements contain in the substrings in this instance that are delimited by one or more characters in parameter value.
In the following example, we initializes a string object with value, whose substrings are separated by '.' (dot). Then we call the String.Split(Char[]) method and pass the parameter value (Char Array) to it. We specify that the string will be splitted by the character '.' (dot) and create an array with elements.
String.Split() method return a new string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array. This member is overloaded, those are String.Split(Char[]), Split(Char[], Int32), Split(Char[], StringSplitOptions), Split(Char[], Int32, StringSplitOptions) and Split(String[], Int32, StringSplitOptions).
In this example, we only discuss about String.Split(Char[]) overloaded method. String.Split(Char[]) method return a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array. This method require to pass a parameter named 'separator' which type is System.Char[]. This parameter value represent an array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters or null.
String.Split(Char[]) method return value type is System.String[] which represent a string array whose elements contain in the substrings in this instance that are delimited by one or more characters in parameter value.
In the following example, we initializes a string object with value, whose substrings are separated by '.' (dot). Then we call the String.Split(Char[]) method and pass the parameter value (Char Array) to it. We specify that the string will be splitted by the character '.' (dot) and create an array with elements.
Split.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void page_Load(object sender, System.EventArgs e) {
if(!this.IsPostBack)
{
TextBox1.Text = "Red.Green.Blue.HotPink.Pink";
TextBox1.Width = 250;
}
}
protected void Button1_Click(object sender, System.EventArgs e) {
string myString = TextBox1.Text.ToString();
char[] separator = new char[] {'.'};
string[] colorList = myString.Split(separator);
ListBox1.DataSource = colorList;
ListBox1.DataBind();
ListBox1.Height = 100;
Label1.Text = "String split and populate ListBox successfully!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to split a string with specific delimiter and populate an array in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net string example: Split</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="HotPink"
Font-Bold="true"
Font-Italic="true"
>
</asp:Label>
<br /><br />
<asp:ListBox
ID="ListBox1"
runat="server"
BackColor="DodgerBlue"
ForeColor="AliceBlue"
>
</asp:ListBox>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
Text="Test String"
ForeColor="DarkGreen"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
BackColor="DarkSeaGreen"
ForeColor="DarkGreen"
>
</asp:TextBox>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Font-Bold="true"
Text="Split String And Populate ListBox"
ForeColor="DarkGreen"
/>
</div>
</form>
</body>
</html>


- String to int
- Get a substring from a string
- Replace a substring of a string
- Join an array elements into a new string
- Compare two strings
- Format a string to add commas in thousands place for a number
- Get only first n characters from a string
- Format a decimal string to currency
- Format a decimal string to int
- Format a string with padding