需要根据优先级
从以下示例字符串中检索一组值string sample="|NR|ABC,777,666,hhh|NR|DEF,444,,|NR|GHI,888,222,"
|NR|
表示新行
string[] priority={"DEF","GHI","ABC","JKL","MNO"}
我需要使用示例字符串检查Priority中的第一个值并检索非空值,如果任何值为空,则需要检查第二个优先级并从示例字符串中检索相应的值
预期结果数组值:444,222,hhh
DEF
值,检索444
并且其余为空,然后检查具有下一优先级的GHI
,检索222
。当第三个值为空时,检查具有下一个优先级的ABC并且'hhh'被重新执行
请在C#Code
中帮助我解决这个问题答案 0 :(得分:1)
|NR|
和,
代码:
string sample="|NR|ABC,777,666,hhh|NR|DEF,444,,|NR|GHI,888,222,";
string[] priority = { "DEF", "GHI", "ABC", "JKL", "MNO" };
string[] result = sample.Split(new[] { "|NR|" }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries))
.OrderBy(x => Array.IndexOf(priority,x.First()))
.Select(x => x.Last())
.ToArray(); //444,222,hhh