编辑
@Ulugbek - 我已经编辑问题以澄清。
我使用模式作为分隔符,需要使用 C# 在模式之间插入 $ 来替换连续出现的模式,但目前它仅替换模式的第一个实例。
除了通过替换运行它几次之外,我还需要做些什么吗?或者,我需要使用 RegEx 吗?
谢谢
String s = "ABCThis is a delimited valueABCABCABCAnd another delimited valueABC";
String.Replace("ABCABC", "ABC$ABC"); //misses second instance of the consecutive patterns instances
结果:
ABCThis is a delimited valueABC$ABCABCAnd another delimited valueABC
期望的结果:
ABCThis is a delimited valueABC$ABC$ABCAnd another delimited valueABC
Ulugbek 解决方案:
ABC$This is a delimited valueABC$ABC$ABC$And another delimited valueABC$
谢谢
最佳答案
您可以重新考虑策略。将所有 ABC
后跟 ABC
替换为 ABC$
。
string s = "ABCABCABC";
string output = Regex.Replace(s, "ABC(?=ABC)", "ABC$");
关于c# - 如何用连续实例替换字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23602841/