对于文件函数,我添加了一个迭代器作为文件的新名称。
名称将如下所示:
- 我的名字
- 我的名字 (1)
- 我的名字 (2)
- ...
- 我的名字 (10)
为了查找下一个“可用”号码,我使用了 while 循环:
while (Stuff.FindNameByText(text) != null)
{
text = string.Format("{0} ({1})", text, i.ToString());
i++;
}
File.SetNewName(text)
但是这段代码当然给了我以下输出:
- 我的名字
- 我的名字 (1)
- 我的名字 (1) (2)
- ...
如何首先在字符串末尾添加一个迭代器(如果不存在),否则替换现有的迭代器。我做了以下操作,但它会以无限循环结束......
while (Stuff.FindNameByText(text) != null)
{
Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", "(", ")"));
text = regex.IsMatch(text, i.ToString());
File.SetNewName(text)
}
也许有人可以帮助我。如果末尾的迭代器不存在 - 添加一个,否则用新数字替换现有的迭代器。
最佳答案
var orig = text;
while (Stuff.FindNameByText(text) != null)
{
text = string.Format("{0} ({1})", orig, i.ToString());
i++;
}
File.SetNewName(text)
关于c# - 替换字符串末尾的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7769928/