簡體   English   中英

C#用隨機生成的字符串替換每個單詞

[英]C# Replacing each words with random generated string

大家好,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);
      }
    }

您可以使用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);

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ç."

這樣,單詞“ quick”,“ fox”,“ lazy”和“ dog”的每個實例將被不同的字符串替換。 在我的示例中,我顯然只是隨機敲擊鍵盤來說明我的觀點,但是如果您有一個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."

就像我說的那樣,由此產生的隨機句子MIGHT很有道理。 但是至少從語法的角度來看這是有意義的。

因為我們的列表將名詞的標記與其相應的有意義的形容詞相匹配,所以我們還可以修改代碼,以便獲得可以保證有意義的隨機結果。

我們要做的就是創建一個名為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為true,我們將始終獲得如下結果: result = "The big brown horse jumps over the sleepy cat.\\r\\nThe lazy brown dog jumps over the small bird."

甚至有可能返回原始句子,因為被替換的單詞也存在於用它們替換的單詞列表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM