I have an ASP.NET page with a multiline textbox called txbUserName. Then I paste into the textbox 3 names and they are vertically aligned:
I want to be able to somehow take the names and split them into separate strings whenever i detect the carriage return or the new line. i am thinking that an array might be the way to go. Any ideas?
thank you.
string[] result = input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
This covers both \n and \r\n newline types and removes any empty lines 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, splitting into a three string array with entries "PersonA", "PersonB" and "PersonC".
Replace any \r\n
with \n
, then split using \n
:
string[] arr = txbUserName.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With