我有这样一个完整的字符串
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:
这个字符串是这样的
- 它是由 ~~ 分隔的 PO(付款选项)列表
- 此列表可能包含一个或多个 OP
- PO 仅包含由 分隔的键值对:
- 空格用++表示
我需要提取键“RGI”和“N”的值。
我可以通过 for loop 做到这一点,我想要一种有效的方法来做到这一点。 对此有任何帮助。
编辑:从~~到~~
最佳答案
不知道它是否比 RegEx 更有效,但这是使用 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();
关于c# - 字符串拆分的有效方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2886966/