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%的几率。问题是如果我运行它将替换所有出现的tst,并且只替换这种情况:
string rt = "tst xtstx xtste tst tst!!";
// /\ /\ /\
有一种更好的方法来替换这些单词吗? 我将在我的代码中多次使用相同的想法。
答案 0 :(得分:3)
您需要使用正则表达式。
匹配所有tst
字词的正则表达式,您需要使用\btst\b
正则表达式。
要替换所有出现,请使用Regex.Replace方法之一。
Here is a something similar example
也不要使用Random(DateTime.Now.Millisecond)
,Random
的默认构造函数的种子比此更好。
示例:
static Random rnd = new Random();
static string Replace(string input, string word, params string[] words)
{
return Regex.Replace(input, "\b" + word + "\b", m=>words[rnd.Next(words.Length)]);
}
现在您可以将其用作:
string rt = "tst xtstx xtste tst tst!!";
if (rnd.Next(3) == 0)
{
rt = Replace(rt, "tst", "something", "nice");
}
if (rnd.Next(3) == 0)
{
rt = Replace(rt, "xtste", "cool", "crazy");
}
答案 1 :(得分:1)
这样做。使用Regex.Replace
和/b
仅使用单词。
Random rnd = new Random(DateTime.Now.Millisecond);
string rt = "tst xtstx xtste tst tst!!";
if (rnd.Next(3) == 0)
{
string[] replaceWords = { "something", "nice" };
rt = Regex.Replace(rt, @"\tst\b", replaceWords[rnd.Next(replaceWords.Length - 1)]);
}
if (rnd.Next(3) == 0)
{
string[] replaceWords = { "cool", "crazy" };
rt = Regex.Replace(rt, @"\xtste\b", replaceWords[rnd.Next(replaceWords.Length - 1)]);
}
做一些重复工作的方法。
public string ReplaceRandom(string[] words, string wordToReplace, string inputString)
{
Random rnd = new Random(DateTime.Now.Millisecond);
inputString = Regex.Replace(inputString, @"\b" + wordToReplace + @"\b", words[rnd.Next(words.Length - 1)]);
return inputString;
}
或者,如果你想使用扩展方法来坚持你可以做的String.Replace
思路。使用rt.ReplaceWithRandom(replaceWords, "tst");
public static string ReplaceWithRandom(this string inputString, string[] words, string wordToReplace)
{
Random rnd = new Random(DateTime.Now.Millisecond);
inputString = Regex.Replace(inputString, @"\b" + wordToReplace + @"\b", words[rnd.Next(words.Length - 1)]);
}
答案 2 :(得分:1)
您可以使用\b
来表示带有正则表达式的单词边界。
另外 - 不要忘记分配回原始字符串!
Random rnd = new Random();
string rt = "tst xtstx xtste tst tst!!";
if (rnd.Next(3) == 0)
{
string[] replaceWords = { "something", "nice" };
rt = Regex.Replace(rt, @"\btst\b", replaceWords[rnd.Next(replaceWords.Length - 1)]);
}
if (rnd.Next(3) == 0)
{
string[] replaceWords = { "cool", "crazy" };
rt = Regex.Replace(rt, @"\bxtste\b", replaceWords[rnd.Next(replaceWords.Length - 1)]);
}
答案 3 :(得分:0)
在我看来,你想为每场比赛想要一个不同的随机词。这是一个为你做这个的课程。
public class RandomStringReplacer
{
// no need to seed this w/ the current time, the default constructor does that already
private readonly Random _random = new Random();
private readonly Regex _regex;
private readonly string[] _replacementStrings;
public RandomStringReplacer(string pattern, params string[] replacementStrings)
{
_regex = new Regex(pattern);
_replacementStrings = replacementStrings.ToArray();
}
// each time we get a replacement string, a randomly selected one is chosen
private string RandomReplacement
{
get { return _replacementStrings[_random.Next(_replacementStrings.Length)]; }
}
public string Replace(string text)
{
var random = new Random();
var result = text;
// get the matches as a stack, because we want to replace backwards so that indexes still match the correct spot in the string
var matches = new Stack<Match>(_regex.Matches(text).OfType<Match>());
while (matches.Count > 0)
{
var match = matches.Pop();
// each match has a 1/3 chance to be replaced
if (random.Next(3) == 0)
{
result = result.Remove(match.Index, match.Length).Insert(match.Index, RandomReplacement);
}
}
return result;
}
}
用法:
var replacements = new[]{"foo", "bar", "FUBAR"};
var pattern = @"(tst)|(xtste)";
var replacer = new RandomStringReplacer(pattern, replacements);
var text = "tst xtstx xtste tst tst!!";
replacer.Replace(text).Dump();