String Split() Method
The following asp.net c# example code demonstrates to us how can we split a String by specified delimiter and populate an array with splitter elements programmatically at run time in an asp.net application. The .net framework String Class represents text as a series of Unicode characters.
The String class’s Split() method returns 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 Split(Char[]), Split(Char[], Int32), Split(Char[], StringSplitOptions), Split(Char[], Int32, StringSplitOptions) and Split(String[], Int32, StringSplitOptions).
In this example, we only discuss the Split(Char[]) overloaded method. The Split(Char[]) method returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array. This method requires passing a parameter named 'separator' which type is System.Char[]. This parameter value represents an array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters or null.
The Split(Char[]) method return’s value type is String[] which represents a String Array whose elements contain the substrings in this instance that are delimited by one or more characters in the parameter value.
In the following example, we initialize a String object with value, whose substrings are separated by '.' (dot). Then we call the Split(Char[]) method and pass the parameter value (Char Array) to it. We specify that the String will be split 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>