Uni Le
Uni Le

Reputation: 793

split String accept some characters

I have some trouble with split string.

My String:

"SG_ PJB_ : 1|10@0+ (0.25,-60) [-60|195.75] "Degrees Celcius"  PCM,TCM,AFCM";

I would like to have:

SG_
PJB_ 
1
10
0+
0.25
-60
-60
195.75
Degrees Celcius
PCM
TCM
AFCM

my Code i have tried:

string s = "SG_ PJB_ : 1|10@0+ (0.25,-60) [-60|195.75] \"Degrees Celcius\"  PCM,TCM,AFCM";
string[] res = s.Split(new char[] { ' ', ',', ':', '|', '@', '(', ')', '[', ']', ';' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < res.Length; i++)
{
   // Console.WriteLine("           ");
    Console.WriteLine("{0}", res[i]);
}
Console.ReadKey();

But it splited "Degrees Celcius" in 2 arrays. what should i do?

Upvotes: 0

Views: 190

Answers (1)

cuongle
cuongle

Reputation: 75296

Use this way:

var result = input.Split(new[] { '"' }).SelectMany((s, i) =>
  {
      if (i%2 == 1) return new[] {s};
      return s.Split(new[] { ' ', ',', ':', '|', '@', '(', ')', '[', ']', ';' },
                               StringSplitOptions.RemoveEmptyEntries);
  }).ToList();

Upvotes: 1

Related Questions