Splitting strings with C# and VB.NET
A textbox can take multiline input when its mode is set to MultiLine. It actually becomes an html textarea element. This example show the use of Split() to break each line of text entered into the box into separate elements of an array. the delimiter is Environment.NewLine, or "\r"
One important note regarding the C# String.Split() method. The string that's passed in as a delimiter needs to be delimited itself with single quotes - not double quotes.
[VB.NET]
'String.Split()
Dim readlines As String()
readlines = TextBox1.Text.Split(Environment.NewLine)
For i As Integer = 0 To readlines.GetUpperBound(0)
Response.Write(readlines(i) + "<br />")
Next
'Regular Expression
Dim readlines As String()
readlines = Regex.Split(TextBox1.Text, Environment.NewLine)
For i As Integer = 0 To readlines.GetUpperBound(0)
Response.Write(readlines(i) + "<br />")
Next
[C#]
//String.Split()
string[] readlines2 = TextBox1.Text.Split('\r');
for (int i = 0; i < readlines2.GetUpperBound(0); i++)
Response.Write(readlines2[i] + "<br />");
//Regular Expression
string[] readlines = Regex.Split(TextBox1.Text,Environment.NewLine);
for(int i = 0; i< readlines.GetUpperBound(0);i++)
Response.Write(readlines[i] + "<br />");
Currently rated 4.50 by 2 people
Rate Now!
Date Posted:
20 May 2007 20:21
Last Updated:
08 May 2008 13:56
Posted by:
Mikesdotnetting
Total Views to date:
8933
Printer Friendly Version


