C# 替换字符串末尾的迭代器
对于文件函数,我在文件的新名称中添加了一个迭代器 名称如下所示:C# 替换字符串末尾的迭代器,c#,string,C#,String,对于文件函数,我在文件的新名称中添加了一个迭代器 名称如下所示: 我的名字 我的名字(1) 我的名字(2) 我的名字(10) 为了找到下一个“可用”号码,我使用了while循环: while (Stuff.FindNameByText(text) != null) { text = string.Format("{0} ({1})", text, i.ToString()); i++; } File.SetNewName(text) 但这段代码当然给了我以下输出: 我的
- 我的名字
- 我的名字(1)
- 我的名字(2)
- 我的名字(10)
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)