【发布时间】:2018-04-20 16:40:19
【问题描述】:
我在 C# 中使用分隔符 && 和 || 拆分字符串时遇到问题。
例如,字符串可能如下所示:
"(abc)&&(rfd)&&(5)||(hh)&&(nn)||(iu)"
代码:
string[] value = arguments.Split(new string[] { "&&" }, StringSplitOptions.None);
我需要在没有() 大括号的情况下拆分或检索数组中的值 - 我需要输出是
"abc" "rfd" "5" "nn" "iu"
我需要它在一个数组中
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("a", "value1");
dict.Add("abc", "value2");
dict.Add("5", "value3");
dict.Add("rfd", "value4");
dict.Add("nn", "value5");
dict.Add("iu", "value6");
foreach (string s in strWithoutBrackets)
{
foreach (string n in dict.Keys)
{
if (s == n)
{
//if match then replace dictionary value with s
}
}
}
///Need output like this
string outputStr = "(value1)&&(value2)&&(value3)||(value4)&&(value5)||(value6)";
【问题讨论】:
-
而预期的输出是。?
-
为什么
" && "中的&&周围有空格? -
您可以编写自己的解析器 - 或查看正则表达式。
-
你已经完成的工作有什么问题?
-
@John - 为什么您希望
new string[] { "&&" }删除||和(和)?那么你需要做什么不是很明显吗?
标签: c#