More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Sunday, 12 April 2009 3:34:15 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 520

Split a string on line breaks in C#?

Split the multiline text on a new line:
1. Use Regular Expression to split on the new line
Code:
string strMultiLines = Textbox1.Text;
string[] strArray = Regex.Split(strMultiLines, Environment.NewLine);
for(int i = 0; i <= strArray.GetUpperBound(0); i++)
  Response.Write(strArray[i] + "<br />");


2. Use a new string of 2 characters "\r\n"to break lines into separate strings via string.split(). Will not return any empty strings, so "None" is an okay value for StringSplitOptions.
Code:
string strArray = strMultiLines.Split(new string[] { "\r\n" }, StringSplitOptions.None);


3. Use a new char[] array of two characters (\r and \n) to break lines into separate strings via string.split(). Use "RemoveEmptyEntries" for StringSplitOptions to ensure no empty strings get put in the string array.
Code:
string[] strArray = strMultiLines.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);


4. Ensure '\n' instead of '\r' is used on a unix/linux system because a text line is terminated by a single "\n" on a unix/linux system, while in DOS/Windows it is "\r\n".
Code:
string[] strArray = strMultiLines.Split('\n');
Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.