Splitting strings with C# and VB.NET

Examples of splitting strings into arrays using C# and VB.Net, both with the String.Split() method, and the Regular Expressions Split() method.

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 />");

Date Posted:
Last Updated:
Posted by:
Total Views to date: 43827

1 Comment

- karthikk

nice

Recent Comments

Justin Kusuma 8/4/2015 7:01 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Hi Mike, thanks for replying... I'm looking for more elegant way to provide user feedback. As now...

Arthur 8/3/2015 4:54 PM
In response to Getting the identity of the most recently added record
Thanks Mike, your site is a mine of useful info :)...

MRP 8/3/2015 4:06 AM
In response to Entity Framework Recipe: Many To Many Relationship On The Same Table
my question is about HashSet and ICollection ! why you use this type and why not user Ilist<> and ?...

Arbaz 8/1/2015 3:39 PM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
Hey Mike, What If I want to set a schedule weekly or every Sunday to run. What would the code for I...

Emma 7/31/2015 3:29 AM
In response to How To Send Email In ASP.NET MVC
As mentioned by John Noble, there seems to be something missing in regards to: return...

Satyabrata Mohapatra 7/30/2015 6:42 AM
In response to Implementing Google's EU End User Consent Policy
Thanks.... Learned a lot....

Jaswanth 7/29/2015 1:31 PM
In response to Displaying Google Analytics Data in ASP.NET
Hi Mike, My application uses MVC 1. I need to fetch the data of pages which has top views. I tried...

Sergey 7/28/2015 6:48 AM
In response to Custom TagHelpers in ASP.NET MVC 6
Thanks a lot!...

Dome 7/27/2015 7:55 PM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Hi, I've just created a new project with WebApi & MVC, then created a controller with the following...

Ted Driver 7/26/2015 5:44 PM
In response to How To Send Email In ASP.NET MVC
Thanks for the great example Mike! I noticed in my implementation of your code that if I put some in...