Random rnd = new Random(DateTime.Now.Millisecond);
string rt = "tst xtstx xtste tst tst!!";
if (rnd.Next(3) == 0)
{
string[] replaceWords = { "something", "nice" };
rt.Replace("tst", replaceWords[rnd.Next(replaceWords.Length - 1)]);
}
if (rnd.Next(3) == 0)
{
string[] replaceWords = { "cool", "crazy" };
rt.Replace("xtste", replaceWords[rnd.Next(replaceWords.Length - 1)]);
}
我想用33%的几率从一个数组中随机取代一个单词。问题是,如果我运行这个它会取代尖沙咀所有出现的,只有更换此事件:检查字符串是否包含单词
string rt = "tst xtstx xtste tst tst!!";
// /\ /\ /\
有一种更好的方式来做到这一点只更换的话? 我将在我的代码中多次使用相同的想法。
@crush replce( “\ BTST \ B”)? –
我对你想要做的事情有点困惑,我想...你能否更好地澄清你的问题? – crush