read a multiline textbox and store each line into an array (c#)

M

Milsnips

Hi there,

Can anyone help me out on this one? i want to read each line entered in a
multiline textbox and store it into an array. I cant find any properties for
this in the control, the only thing i can think of is to use Text.Split and
split the string at each carriage return,

Any help appreciated.
thanks.
Paul
 
M

Michael D. Ober

Take a look at the TextBox.Lines property. It gets/sets an array of strings
(as opposed to a StringArray)

Mike Ober.
 
M

Milsnips

Hi Mike,

only problem is im doing it in c# ASP.NET and .Lines property is not
available here, i think its only in Windows Forms.

regards,
Paul
 
M

Milsnips

Never mind, i found a solution using
System.Text.RegularExpressions.Regex.Split

It allows splitting by a string, instead of a char array.

regards,
Paul
 
M

Michael D. Ober

Instead of using the regex split, you might want to use the string.Split
method - it's optimized for strings.

Mike.
 
C

Chris Dunaway

Milsnips said:
Never mind, i found a solution using
System.Text.RegularExpressions.Regex.Split

It allows splitting by a string, instead of a char array.

Another solution to try if you need to loop through the strings is a
StringReader.

Chris
 
M

Michael D. Ober

Unless C# has the equivalant of the VB vbNewLine or the C/C++ '\n' character
strings, this should be the easiest way to split a string. I know that '\n'
is a character and not a string in C/C++. However, the framework's
string.split function takes both.

I would try replacing the second line with

string[] b = a.Split("\n");

If that doesn't work, try

string[] b = a.Split(Environment.Newline)

In either case, the string.Split method will work with a string or a
character.

Mike Ober.

Bobbo said:
Instead of using the regex split, you might want to use the string.Split
method - it's optimized for strings.

Unless anybody knows of any reason why it should not be used in this
way, I find the following syntax works fine:

string a = (some string with newlines in it)
string[] b = a.Split(new string[] { Environment.NewLine},
StringSplitOptions.None);

http://msdn2.microsoft.com/en-us/library/tabh47cf.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top