字符串开始我有一个字符串,它看起来像这样替换字符串,如果在列表
s = "<Hello it´s me, <Hi how are you <hay"
和列表 List<string> ValidList= {Hello, hay}
我需要的结果字符串要像
string result = "<Hello it´s me, ?Hi how are you <hay"
这样结果字符串如果它开始于<,其余的信号到列表中,保留它,否则,如果以<开头,但不是列表中的H替代H?
我尝试使用的IndexOf找到<的位置,如果字符串后starsWith列表中的任何字符串的离开它。
foreach (var vl in ValidList)
{
int nextLt = 0;
while ((nextLt = strAux.IndexOf('<', nextLt)) != -1)
{
//is element, leave it
if (!(strAux.Substring(nextLt + 1).StartsWith(vl)))
{
//its not, replace
strAux = string.Format(@"{0}?{1}", strAux.Substring(0, nextLt), strAux.Substring(nextLt + 1, strAux.Length - (nextLt + 1)));
}
nextLt++;
}
}
'Regex.Replace(S,的String.Format( “<({0})?!” 的string.join( “|”,ValidList) ),“?”)' – poke
但是在有这个字符串的情况下s =“<你好,它是我的
你根本没有指定什么应该发生在不是一个词的前缀的<<处。你也没有举一个例子。你也没有提到我能想到的所有其他边缘案例。 – poke