c# example - convert stringbuilder to string array
The following asp.net c# example code demonstrate us how can we convert
a StringBuilder objectto a String Array programmatically at run time in
an asp.net application. .Net framework's StringBuilder Classrepresent a
mutable string of characters.
StringBuilder class has no built in method or property to convert a StringBuilder object to a String Array object.We also cannot directly convert a StringBuilder object to a String Array object.
To convert a StringBuilder object to a String Array, at first we need to convert the StringBuilder object to a String object.StringBuilder.ToString() method allow us to convert the value of a StringBuilder object to a string which data type isSystem.String.
String Class String.Split(Char[]) overloaded method return a String Array that contains the substrings in this instancethat are delimited by elements of a specified Unicode character array. String.Split(Char[]) method require to pass aparameter named 'separator' which value type is System.Char[].
The parameter represent an array of Unicode charactersthat delimit the substrings in this instance, an empty array that contains no delimiters or null.String.Split() method return an array object as System.String[].
In this example code, we uses an empty space (white space/blank space) as delimiter of StringBuilder object.The Split() method return an array object with elements those are generated from StringBuilder characters splitby white space delimiter. Finally, we convert a StringBuilder object to a String Array with elements splitby specified delimiter.
StringBuilder class has no built in method or property to convert a StringBuilder object to a String Array object.We also cannot directly convert a StringBuilder object to a String Array object.
To convert a StringBuilder object to a String Array, at first we need to convert the StringBuilder object to a String object.StringBuilder.ToString() method allow us to convert the value of a StringBuilder object to a string which data type isSystem.String.
String Class String.Split(Char[]) overloaded method return a String Array that contains the substrings in this instancethat are delimited by elements of a specified Unicode character array. String.Split(Char[]) method require to pass aparameter named 'separator' which value type is System.Char[].
The parameter represent an array of Unicode charactersthat delimit the substrings in this instance, an empty array that contains no delimiters or null.String.Split() method return an array object as System.String[].
In this example code, we uses an empty space (white space/blank space) as delimiter of StringBuilder object.The Split() method return an array object with elements those are generated from StringBuilder characters splitby white space delimiter. Finally, we convert a StringBuilder object to a String Array with elements splitby specified delimiter.
convert-stringbuilder-to-string-array.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder stringb = new StringBuilder();
stringb.Append("Cornsilk DarkSalman Gold Gray Green");
Label1.Text = "stringbuilder: " + stringb.ToString();
string[] stringArray = stringb.ToString().Split(' ').ToArray();
Label1.Text += "<br /><br />loop through string array...............";
foreach (string s in stringArray)
{
Label1.Text += "<br />" + s;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - convert stringbuilder to string array</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - convert stringbuilder to string array
</h2>
<hr width="550" align="left" color="Gainsboro" />
<br />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="convert stringbuilder to string array"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>