Splitting strings with C# and VB.NET

3.67 (9 votes)

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.

Dim readlines As String()
readlines = TextBox1.Text.Split(Environment.NewLine)
For i As Integer = 0 To readlines.GetUpperBound(0)
  Response.Write(readlines(i) + "<br />")

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

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: 44599

1 Comment

- karthikk


Recent Comments

Phil 29/11/2015 18:48
In response to iTextSharp - Working with images
Hi Mike, great article as ever. Any chance you can do an example of adding a swf to a pdf with I it...

makhlas 27/11/2015 17:05
In response to How To Send Email In ASP.NET MVC
hello, thanks for sharing these tutos , but when i apply the send button, i'm gettin this error...

raphael 27/11/2015 13:59
In response to ASP.NET 5: Managing Client-side Dependencies with NPM, Bower and Gulp
i fixed it. i'm not sure *what* it is that VS does, but you get the same result if you do a "npm on...

Raphael 27/11/2015 13:02
In response to ASP.NET 5: Managing Client-side Dependencies with NPM, Bower and Gulp
There seems to be a major issue with the dependency manager in VS2015. I haven't yet figured out but...

Joaquín Bresan 26/11/2015 17:58
In response to Getting the identity of the most recently added record
Great contribution friend. Thank you so much for sharing....

JR 26/11/2015 02:49
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Hello, Where is the location of the database? Thanks...

sumalatha 24/11/2015 22:09
In response to ASP.NET 5 Middleware, Or Where Has My HttpModule Gone?
Great article. I have gone through hundreds of articles, finally this gives the comprehensive of...

TimPat10 24/11/2015 17:35
In response to A Better Way To Export Gridviews To Excel
Great article. I've noticed that date fields have to be re-formatted when the spreadsheet is pulled...

Rajasekar 24/11/2015 12:27
In response to Import Data From Excel to Access with ASP.NET
While use this code i'm facing on error: "Unrecognized Database format C:\mydabase.accdb" can any...

Parmod 24/11/2015 07:28
In response to ASP.NET 5 Project Basics
For a new learner (Fresher) in ASP.NET there is a issue Fresher have to learn two types of , old...