字符串開始我有一個字符串,它看起來像這樣替換字符串,如果在列表
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