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:
- Jason
- Ammy
- Karen
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.
It depends what you want to do. Another option, which is probably overkill for small lists, but may be more memory efficient for larger strings, is to use the
StringReader
class and use an enumerator:This supports both
\n
and\r\n
line-endings. As it returns anIEnumerable
you can process it with aforeach
, or use any of the standard linq extensions (ToList()
,ToArray()
,Where
, etc).For example, with a
foreach
:Replace any
\r\n
with\n
, then split using\n
:String.Split
?Try this:
Works if :
Or
Or
This covers both \n and \r\n newline types and removes any empty lines your users may enter.
I tested using the following code:
And it works correctly, splitting into a three string array with entries "PersonA", "PersonB" and "PersonC".