Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective way of String splitting

I have a completed string like this

N:Pay in Cash++RGI:40++R:200++T:Purchase++IP:N++IS:N++PD:PC++UCP:598.80++UPP:0.00++TCP:598.80++TPP:0.00++QE:1++QS:1++CPC:USD++PPC:Points++D:Y++E:Y++IFE:Y++AD:Y++IR:++MV:++CP:~~ N:ERedemption++RGI:42++R:200++T:Purchase++IP:N++IS:N++PD:PC++UCP:598.80++UPP:0.00++TCP:598.80++TPP:0.00++QE:1++QS:1++CPC:USD++PPC:Points++D:Y++E:Y++IFE:Y++AD:Y++IR:++MV:++CP:

this string is like this

  1. It's list of PO's(Payment Options) which are separated by ~~
  2. this list may contains one or more OP
  3. PO contains only Key-Value Pairs which separated by :
  4. spaces are denoted by ++

I need to extract the values for Key "RGI" and "N".

I can do it via for loop , I want a efficient way to do this. any help on this.

Edit: from ~ ~ To ~~

like image 305
Posto Avatar asked Jan 23 '23 00:01

Posto


2 Answers

Don't know if it's more efficient than RegEx, but here's a alternative using LINQ to Objects.

KeyValuePair<string, string>[] ns = (from po in pos.Split(new string[] { "~~" }, StringSplitOptions.RemoveEmptyEntries)
                                     from op in po.Split(new string[] { "++" }, StringSplitOptions.RemoveEmptyEntries)
                                     where op.StartsWith("N:") || op.StartsWith("RGI:")
                                     let op_split = op.Split(':')
                                     select new KeyValuePair<string, string>(op_split[0], op_split[1])).ToArray();
like image 60
Franci Penov Avatar answered Jan 24 '23 14:01

Franci Penov


I think you should try a regular expression. Since you are using C#, check out this handy .NET RegEx cheat sheet.

like image 45
Matthew Groves Avatar answered Jan 24 '23 13:01

Matthew Groves