How to split lines into carriage return using C #?

I have an ASP.NET page with a multi-line text field called txbUserName. Then I paste into the text box 3 names and aligns vertically:

  • Jason
  • Ammy
  • Karen

I want to be able to somehow take the names and split them into separate lines whenever I detect a carriage return or a new line. I think an array may be the way to go. Any ideas?

Thank you.

+52
c # text-manipulation
Nov 29 '09 at 3:36
source share
7 answers
string[] result = input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries); 

This covers the types \ n and \ r \ n newline and removes any empty lines that your users may enter.

I tested using the following code:

  string test = "PersonA\nPersonB\r\nPersonC\n"; string[] result = test.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries); foreach (string s in result) Console.WriteLine(s); 

And it works correctly, breaking into a three-string array with entries "PersonA", "PersonB" and "PersonC".

+119
Nov 29 '09 at 3:40
source share
β€” -

Replace any \r\n with \n , then split with \n :

 string[] arr = txbUserName.Text.Replace("\r\n", "\n").Split("\n".ToCharArray()); 
+6
Nov 29 '09 at 3:53
source share

Take a look at the String.Split function (not sure about the exact syntax, there is no IDE in front of me).

 string[] names = txbUserName.Text.Split(Environment.Newline); 
+4
Nov 29 '09 at 3:41
source share

String.Split ?

 mystring.Split(new Char[] { '\n' }) 
+1
Nov 29 '09 at 3:40
source share
 using System.Text; using System.Text.RegularExpressions; protected void btnAction_Click(object sender, EventArgs e) { string value = txtDetails.Text; char[] delimiter = new char[] { ';','[' }; string[] parts = value.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < parts.Length; i++) { txtFName.Text = parts[0].ToString(); txtLName.Text = parts[1].ToString(); txtAge.Text = parts[2].ToString(); txtDob.Text = parts[3].ToString(); } } 
+1
Oct 20 '11 at 6:22
source share

Try the following:

 message.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 

It works if:

 var message = "test 1\r\ntest 2"; 

or

 var message = "test 1\ntest 2"; 

or

 var message = "test 1\rtest 2"; 
0
Apr 21 '17 at 9:25
source share

It depends on what you want to do. Another option, which is probably redundant for small lists, but might be more memory efficient for large strings, is to use the StringReader class and use an enumerator:

 IEnumerable<string> GetNextString(string input) { using (var sr = new StringReader(input)) { string s; while ((s = sr.ReadLine()) != null) { yield return s; } } } 

This supports both \n and \r\n line endings. Since it returns IEnumerable you can process it using foreach or use any of the standard linq extensions ( ToList() , ToArray() , Where , etc.).

For example, with foreach :

 var ss = "Hello\nworld\r\ntwo bags\r\nsugar"; foreach (var s in GetNextString(ss)) { Console.WriteLine("==> {0}", s); } 
0
Jan 08 '19 at 20:51
source share



All Articles