我有一个 String ,其中包含 Word 不止一次 .. 我想一个接一个地找到该单词的每次出现,并需要在 That 之后添加一些其他单词 ..
例子 -
<start>
<child1>
.
.
.
<start>
<child2>
..
等等 ..
我试过 。
int count = Regex.Matches(StrRemoveWrongData, @"<start>").Count;
它让我数数,有任何事情我们可以一一进行。并在其下方添加新词..
我有一个 String ,其中包含 Word 不止一次 .. 我想一个接一个地找到该单词的每次出现,并需要在 That 之后添加一些其他单词 ..
例子 -
<start>
<child1>
.
.
.
<start>
<child2>
..
等等 ..
我试过 。
int count = Regex.Matches(StrRemoveWrongData, @"<start>").Count;
它让我数数,有任何事情我们可以一一进行。并在其下方添加新词..
你可以简单地做:StrRemoveWrongData = StrRemoveWrongData.Replace(@"<start>", newString);
根据你的标题,获取所有索引;
var indexList = Regex.Matches(StrRemoveWrongData, @"<start>").Cast<Match>()
.Select(m => m.Index)
.ToList();
如果您的替换算法比简单的字符串更复杂,您可以使用 Regex.Replace 的重载
int count = 1;
var newstr = Regex.Replace(StrRemoveWrongData, @"<start>",
m => {
//Do some work
return String.Format("<child{0}>",count++);
}
);