我正在尝试用
替换每个 --[[RANDOMNUMBER and textbox1 Text]]
,但是如何为每次替换选择一个新数字,所以它并不总是 241848 之类的?
Random random = new Random();
string replacing = " --[[" + random.Next() + textBox1.Text + "]] ";
string output = richTextBox1.Text.Replace(" ", replacing);
最佳答案
请改用 Regex.Replace(String, String, MatchEvaluator)
。它需要一个 MatchEvaluator
回调函数,您可以在其中提取下一个随机数:
Random random = new Random();
string output = Regex.Replace(richTextBox1.Text, " ", (match) =>
string.Format(" --[[{0}{1}]] ", random.Next(), textBox1.Text));
例如:
Random random = new Random();
string output = Regex.Replace("this is a test", " ", (match) =>
string.Format(" --[[{0}{1}]] ", random.Next(), "sample"));
上面的示例输出:
关于c# - 如何用不同的数字替换每个字符串匹配?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25851885/