C#替换每个替换+不同的数字

use*_*921 2 c# string replace numbers

我试图用" - [[RANDOMNUMBER和textbox1 Text]]替换每个""但是我如何为每个替换选择一个新的数字?所以它并不总是241848等.谢谢!

Random random = new Random();
string replacing = " --[["+random.Next()+textBox1.Text+"]] ";
string output = richTextBox1.Text.Replace(" ", replacing);
Run Code Online (Sandbox Code Playgroud)

Cᴏʀ*_*ᴏʀʏ 6

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));
Run Code Online (Sandbox Code Playgroud)

例如:

Random random = new Random();
string output = Regex.Replace("this is a test", " ", (match) => 
    string.Format(" --[[{0}{1}]] ", random.Next(), "sample"));
Run Code Online (Sandbox Code Playgroud)

上面的示例输出:

this --[[1283057197sample]] is --[[689040621sample]] a --[[1778328590sample]] test