[英]How do I replace a substring that appears more than once in a string using a while loop?
我有一个字符串,其中包含我需要查找和替换的两个相同的子字符串。
我使用了Contains()
方法,如果子字符串只出现一次,该方法可以正常工作。
string test = "abc";
if (line.Contains(test))
{
string newLine = line;
while (line.Contains(test))
{
newLine = newLine.Replace(test, "Hello");
}
}
我基本上需要一些可以让我摆脱循环的东西。 比如正则表达式中的nextMatch。
除非我误解了,否则您正在尝试替换所有测试实例? 只需这样做:
line = line.Replace(test, "Hello")
不需要 while 或循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.