2014-01-07 80 views
2
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!!"; 
//   /\    /\ /\ 

有一种更好的方式来做到这一点只更换的话? 我将在我的代码中多次使用相同的想法。

+0

@crush replce( “\ BTST \ B”)? –

+0

我对你想要做的事情有点困惑,我想...你能否更好地澄清你的问题? – crush

回答

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"); 
} 
+0

有一个干净的方法来做到这一点?因为我要重复多次重复相同的想法? –

+0

@Gorfi,编辑示例 –

+0

我需要多个如果使用Radom rnd这2行? –

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)]); 
} 
+0

因为我要多次使用此代码有一种方法可以使它更清洁? –

+0

@Gorfi你想把这个功能拉出到一个单独的函数中,或者只是缩短代码? –

+0

只需轻松实现一个带有replaceWords数组的新wordToReplace即可。 –

1

可以使用\b表示与正则表达式的字boundry。

另外 - 不要忘记分配回原来的字符串!

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)]); 
} 
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(); 
相关问题