1

I'm trying to separate the values from an asp.net textbox which ends with line breaks. For example -

959100001
959100002

Those values must be inserted into an array like {95910001, 959100002} to do further calculation.

Any advices?

I'm using C# btw.

5 Answers 5

3
string test = "959100001\r\n959100002\r\n";

foreach(var item in test.Split(new char []{'\r','\n'},StringSplitOptions.RemoveEmptyEntries))
Console.WriteLine(item);

Prints:

959100001
959100002

Or as suggested by Mike:

test.Split(new string[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries))
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use String.split() method which split string based upon newline delimiter and return string[] array. Further you can use long.TryParse or int.TryParse method to convert string to number (int/long) type.

1 Comment

And maybe Environment.NewLine Property msdn.microsoft.com/en-us/library/…
1

I had tried this in my environment. See below....

enter image description here

My code snippet for your question's answer is below.

string[] lines = txtline.Text.Split(new string[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);

It works fine.....

Comments

0

Not tested:

string[] lines = TextBox1.Text.Split(Environment.NewLine);

Comments

0

if you just want to separate them, you can simply call Split method:

string[] lines = TextBox1.Text.Split('\n', '\r');

if you also want to convert these values to integer, try this:

int[] lines = Array.ConvertAll<string, int>(TextBox1.Text.Split('\n', '\r'), Convert.ToInt32);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.