Search:
Welcome Guest | Register | Login
logo

How to split a string on the basis of new line in C#

How to split a string on the basis of new line in C#

String str = @"X
Y
Z
P
Q
R"

So resultant string array = X|Y|Z|P|Q|R    
Length = 6

I tried some but could not fixed my issue, please help me to solve my problem!

  • C#
  • ASP.Net
1
 
Asked: 03 Mar 2013
Reputation: 79
Grant Marino
2 Answers
String[] result = str.Split(new string[] { "\r\n" }, StringSplitOptions.None)

It will give your required result.

1
 
Answered: 03 Mar 2013
Reputation: 125
Daniel Hacker

User "\n" and "\r\n", it will split on both case as will remove blank lines as well.

String[] result = str.Split(new string[] {"\n", "\r\n"},
                             StringSplitOptions.RemoveEmptyEntries);

Or simply try this

String[] result = str.Split(Environment.Newline);
0
 
Answered: 04 Mar 2013
Reputation: 296
Jason Olivera
Login to post your answer