C# 用随机生成的字符串替换每个单词
StackOverFlow的人们好,下面我写的错误代码有问题。我的问题是如何用随机生成的字符串替换下面的每个单词 我的问题是下面的代码,现在它正在随机替换所有的单词,但是它们都是相同的,我想要的是用随机生成的字符串替换它们,而不是所有的单词都是相同的 我已经在参考资料中多次使用这些字符串 我在参考资料中的话是:basenet、R02、R01、R03、R0、vistubularcut、naturalipad、braskausa、moonsteelir、dubilomo 我的代码: 公共字符串randogen1int a1,随机{ StringBuilder result1=新StringBuilderR1; 字符串=textBox1.Text; 对于int i=0;iC# 用随机生成的字符串替换每个单词,c#,random,replace,C#,Random,Replace,StackOverFlow的人们好,下面我写的错误代码有问题。我的问题是如何用随机生成的字符串替换下面的每个单词 我的问题是下面的代码,现在它正在随机替换所有的单词,但是它们都是相同的,我想要的是用随机生成的字符串替换它们,而不是所有的单词都是相同的 我已经在参考资料中多次使用这些字符串 我在参考资料中的话是:basenet、R02、R01、R03、R0、vistubularcut、naturalipad、braskausa、moonsteelir、dubilomo 我的代码: 公共字符串ran
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.
现在,让我们创建列表:
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.
然后我们相应地修改代码:
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.
现在,如果GuaranteMatch为真,我们将始终得到如下结果:
结果=棕色的大马跳过昏昏欲睡的猫。\r\n懒惰的棕色狗跳过小鸟
它甚至可以返回原来的句子,因为被替换的单词也存在于要替换的单词列表中。你想要的东西列表不是一个问题。你的问题是什么?这不是调试错误代码的服务;如果你有一个明确的、具体的问题并有答案,就去问这个问题选项。如果您想了解如何调试错误代码的建议,请参阅@Ashlou foreach是C中该类型循环的合适关键字。您的编辑将其更改为for each,这是不正确的。编辑其他人的代码时请小心,尤其是在问题中。@EricLippert好吧,我的问题是如何用随机泛型替换这些单词ted字符串。您希望用作替换的字符串是随机的,还是希望随机选择数组中的一个字符串作为替换字符串?@BMinster基本上我希望替换所有字符串,并且希望所有字符串都是随机的。请注意,您可能希望将随机实例设为私有静态;这样,如果方法被快速连续多次调用。您不会得到相同的输出。请注意,如果长度较长,则效率会非常低。最好是创建一个具有适当长度的字符数组,或使用字符串生成器。请注意,修改其输入的方法更难调试。并且,您的长度变量不再按照它所说的那样执行;现在它意味着要添加的字符,而不是字符串长度。考虑使用本地剩余而不是修改长度。注意,65和90是魔术数字。如果您使用的是“A”而不是65,代码变得更清晰。“EricLippert谢谢,做了一个编辑来改进我的答案。
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.
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.