Friday 15 February 2013

String split by Newline in C#

String split by Newline in C#

if String is in this format
          String str = @"1)ABS
2)SIGN
3)MOD
4)FLOOR
5)CEIL
6)ROUND
7)EXP
8)LOG
9)LOG10
10)POW
11)POWER
12)SQRT
13)PI
14)COS
15)SIN
16)TAN";

Split string by new line -> then  by  ')'  get exact Strings 


1) Split by Newline
            User can split by  "\r\n"   or System.Environment.NewLine

          String final=String.Empty;
           String[] array=str.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
           foreach (String s in array)
           {
               Console.WriteLine("--"+s+"--");
               final+=s.Split(new char[] {')'}, StringSplitOptions.RemoveEmptyEntries)[1]+" ";
           }

OUTPUT
1)ABS
2)SIGN
3)MOD
4)FLOOR
5)CEIL
6)ROUND
7)EXP
8)LOG
9)LOG10
10)POW
11)POWER
12)SQRT
13)PI
14)COS
15)SIN
16)TAN
 
2) Split String by  ')'  character

           String[] finalwords=final.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);
           Array.Sort(finalwords);
           int i = 0;
           foreach (String f in finalwords)
           {
               Console.Write(f + "\t");
           }

FINAL OUTPUT:

ABS SIGN MOD FLOOR CEIL ROUND EXP LOG LOG10 POW POWER SQRT PI COS SIN TAN
ABS     CEIL    COS     EXP     FLOOR   LOG     LOG10   MOD     PI      POW
POWER   ROUND   SIGN    SIN     SQRT    TAN

Tags:String split by Newline in C#,String split by '\r\n',String Split by System.Environment.NewLine. Split Strings in C#,Split a String on Line Breaks,Split a String on carriage Return

No comments:

Post a Comment