首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C#将每个单词替换为随机生成的字符串

C#将每个单词替换为随机生成的字符串
EN

Stack Overflow用户
提问于 2018-05-31 05:21:36
回答 2查看 312关注 0票数 -1

大家好,StackOverFlow的朋友们,我对下面我写的错误代码有一个问题。我的问题是如何用随机生成的字符串替换下面的每个单词。

我的问题是,下面的代码现在是随机地替换所有的单词,但它们都是相同的,我想要的是用随机生成的字符串替换它们中的每一个,而不是全部相同。

我已经在参考资料中多次使用了这些字符串。

我的资源是:"basenet","R02","R01","R03","R0","vistubularcut","naturalipad","braskausa","moonsteelir","dubilomo“

我的代码:

public string RandomGen1(int a1, Random random) {
  StringBuilder result1 = new StringBuilder(a1);
  string characters = textBox1.Text;
  for (int i = 0; i < numericUpDown1.Value; i++) {
    result1.Append(characters[random.Next(characters.Length)]);
  }
  return result1.ToString();
}

private void button1_Click(object sender, EventArgs e) {
    Random rnd = new Random();
    string[] gen1 = new string[1];
    for (int a = 0; a < gen1.Length; a++) {
      gen1[a] = RandomGen1(1, rnd);
      string source = (String.Join(Environment.NewLine, gen1));
      string executionerv2 = Properties.Resources.String1;

      string[] replace1 = {
        "basenet",
        "R02",
        "R01",
        "R03",
        "R0",
        "vistubularcut",
        "naturalipad",
        "braskausa",
        "moonsteelir",
        "dubilomo"
      };

      foreach (string curr in replace1) {
        executionerv2 = executionerv2.Replace(curr, source);
      }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-31 05:42:23

您可以使用Random生成随机数,将其转换为char并附加到string

private string RandomString(int length)
    {
        Random rdn = new Random();
        string toReturn = string.Empty;
        while (length > 0)
        {
            toReturn += (char)rdn.Next(65, 90);
            length--;
        }

        return toReturn;
    }

我根据ASCII表选择了范围:https://www.asciitable.com/,如果您还想要一个随机的length,那么只需在调用方法中创建一个新的Random实例即可。

编辑:

基于评论,这里有一个更好的方法。

private static Random rdn = new Random();
private string RandomString(int length)
{
    return new string((char)rdn.Next('A', 'Z'), length);
}

最后,您可以简单地编写:replace1[index] = RandomString(4);

票数 2
EN

Stack Overflow用户

发布于 2018-05-31 06:58:26

String.Replace(string, string)将用第二个参数字替换第一个参数字的每个实例。如果你想用不同的字符串替换同一个单词的每个单独的实例,你必须分离字符串的单词,迭代它们,每次你找到想要替换的单词时,用随机字符串替换它。

下面是一个例子(假设你已经有了一个获取随机单词的方法):

string Source = "The quick brown fox jumps over the lazy dog.\r\nThe quick brown fox jumps over the lazy dog.";
string[] ReplaceWords = { "quick", "fox", "lazy", "dog" };

string[] SourceWords = Source.Split(' ');
string result = "";
for (int i = 0; i < SourceWords.Length; i++)
{
    if (ReplaceWords.Contains(SourceWords[i]))
        result += " " + GetRandomWord();
    else
        result += " " + SourceWords[i];
}
result = result.Trim() //Remove white spaces at the begining and end of the string.

result = "The fdhs brown fdsfsd jumps over the hgfih turioutro.\r\nThe cxnvcxn brown oipuop jumps over the rewrre kmlçnlç."

这样,单词“快速”、“狐狸”、“懒惰”和“狗”的每个实例都将被不同的字符串替换。在我的示例中,我显然只是随机地敲击键盘来说明我的观点,但是如果您有一个从列表中获取现有单词的GetRandomWord函数,它也可以工作:

result = "The tree brown house jumps over the grass shell.\r\nThe hidrant brown mushroom jumps over the ocean skateboard."

我的例子都是迟钝的,但它们说明了我的观点。

我希望我能帮上忙:)

只是为了好玩

如果你做一个从现有列表中选择单词的GetRandomWord,并且它是上下文感知的,你可以得到可能实际上有意义的句子。

让我们为我们的上下文创建一个enum。为了简单起见...让我们保持简单:

enum Context
{
    Adjective,
    Noun
}

现在让我们创建我们的列表:

string[] Nouns = {"dog", "cat", "fox", "horse", "bird"};
string[] Adjectives {"lazy", "sleepy", "quick", "big", "small"};

现在来看我们的方法:

string GetRandomWord(Context c)
{
    Random R = new Random();
    switch (c)
    {
        case Context.Noun:
            return Nouns[R.Next(0, Nouns.Length)];
            break;
        case Context.Adjective:
            return Adjectives[R.Next(0, Adjectives.Length)];
            break;
    }
}

现在稍微修改一下替换代码的文本:

string Source = "The quick brown fox jumps over the lazy dog.\r\nThe quick brown fox jumps over the lazy dog.";
string[] ReplaceAdjectives = { "quick", "lazy" };
string[] ReplaceNouns = { "fox", "dog" };

string[] SourceWords = Source.Split(' ');
string result = "";
for (int i = 0; i < SourceWords.Length; i++)
{
    if (ReplaceAdjectives.Contains(SourceWords[i]))
        result += " " + GetRandomWord(Context.Adjective);
    else if (ReplaceNouns.Contains(SourceWords[i]))
        result += " " + GetRandomWord(Context.Noun);
    else
        result += " " + SourceWords[i];
}
result = result.Trim() //Remove white spaces at the begining and end of the string.

result = "The sleepy brown bird jumps over the small horse.\r\nThe lazy brown cat jumps over the sleepy dog."

正如我所说的,通过这种方式,产生的随机句子可能会有一定的意义。但至少从语法的角度来看,这是有意义的。

因为我们的列表将名词的索引与其相应的有意义的形容词相匹配,所以我们还可以修改代码,以便获得保证有意义的随机结果。

我们需要做的就是创建一个名为GetMatchingWord(Context, int)的新方法。它接受一个int,因为它不再将单词选择本身随机化。现在,这是在调用方法中完成的。

string GetMatchingWord(Context c, int i)
{
    switch (c)
    {
        case Context.Noun:
            return Nouns[i];
            break;
        case Context.Adjective:
            return Adjectives[i];
            break;
    }
}

然后我们相应地修改代码:

string Source = "The quick brown fox jumps over the lazy dog.\r\nThe quick brown fox jumps over the lazy dog.";
string[] ReplaceAdjectives = { "quick", "lazy" };
string[] ReplaceNouns = { "fox", "dog" };

bool GuaranteeMatch = true;

string[] SourceWords = Source.Split(' ');
string result = "";
Random R = new Random();
for (int i = 0; i < SourceWords.Length; i++)
{
    if (GuaranteeMatch)
    {
        int I = R.Next(0, Adjectives.Length) //Adjectives and Nouns have the same Length. This is a requirement for this method to work.
        if (ReplaceAdjectives.Contains(SourceWords[i]))
            result += " " + GetMatchingWord(Context.Adjective, I);
        else if (ReplaceNouns.Contains(SourceWords[i]))
            result += " " + GetMatchingWord(Context.Noun, I);
        else
            result += " " + SourceWords[i];
    }
    else
    {
        if (ReplaceAdjectives.Contains(SourceWords[i]))
            result += " " + GetRandomWord(Context.Adjective);
        else if (ReplaceNouns.Contains(SourceWords[i]))
            result += " " + GetRandomWord(Context.Noun);
        else
            result += " " + SourceWords[i];
    }
}
result = result.Trim() //Remove white spaces at the begining and end of the string.

现在,如果GuaranteeMatch为真,我们将总是得到如下结果:result = "The big brown horse jumps over the sleepy cat.\r\nThe lazy brown dog jumps over the small bird."

它甚至可以返回原始句子,因为被替换的单词也存在于替换它们的单词列表中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50613519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档