C# 检查字符串是否包含单词
我想用概率为33%的数组中的随机词替换一个词。问题是,如果我运行此命令,它将替换tst的所有事件,并且仅替换此事件:C# 检查字符串是否包含单词,c#,regex,arrays,string,replace,C#,Regex,Arrays,String,Replace,我想用概率为33%的数组中的随机词替换一个词。问题是,如果我运行此命令,它将替换tst的所有事件,并且仅替换此事件: 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"
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)]);
}
有更好的方法来做到这一点,只需替换单词?
我将在代码中多次使用相同的想法。这样做。使用
Regex.Replace
和/b
仅使用单词
string rt = "tst xtstx xtste tst tst!!";
// /\ /\ /\
方法做一些重复的工作
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)]);
}
或者,如果您想使用扩展方法来坚持使用字符串。替换您可以做的思路。用rt.ReplaceWithRandom(replaceWords,“tst”)调用它代码>
您需要为此使用正则表达式
匹配所有tst
单词的正则表达式,您需要使用\btst\b
Regex
要替换所有发生的事件,请使用以下方法之一
也不要使用Random(DateTime.Now.millished)
,默认的Random
构造函数的种子比这个更好
例如:
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)]);
}
现在您可以这样使用它:
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)]);
}
可以使用\b
用正则表达式表示单词边界
还有-别忘了重新分配给原始字符串
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");
}
在我看来,每次比赛你都需要一个不同的随机词。这里有一个课程可以帮你做这件事
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)]);
}
@粉碎回复(“\btst\b”)?我对你到底想做什么有点困惑,我想…你能进一步澄清你的问题吗?因为我要多次使用此代码,有一种方法可以让它更干净?@Gorfi你是想将此功能拉到一个单独的函数中,还是只想使代码更短?只需简单地实现一个新单词即可replaceWords数组。@Gorfi我添加了一个函数,看看这是否就是您要寻找的。为什么要使用x=>words[rnd.Next(words.Length)]而不是words[rnd.Next(words.Length)])有一个干净的方法来实现这一点?既然我要多次复制同一个想法?我需要使用Radom rnd这两行的多个if?为什么要使用m=>words[rnd.Next(words.Length)]而不是words[rnd.Next(words.Length)]?当你使用words[rnd.Next(words.Length)]
时,你传递一个选定的单词,它会用这个单词替换所有找到的单词。但是当您传递m=>words[rnd.Next(words.Length)]
时,您将传递一个函数,该函数将为每个找到的匹配项调用以选择替换项,因此每个替换项都将随机进行
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)]);
}
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();