1

我尝试在 Form1 中这样做:

private void BtnScrambleText_Click(object sender, EventArgs e)
{
    textBox1.Enabled = false;
    BtnScrambleText.Enabled = false;

    StringBuilder sb = new StringBuilder();
    var words = textBox1.Text.Split(new char[] { ' ' });
    foreach (var w in words)
    {
        if (w == " ")
        {
            sb.Append(w);
            continue;
        }

        ScrambleTextBoxText scrmbltb = new ScrambleTextBoxText(w);
        scrmbltb.GetText();
        sb.Append(scrmbltb.scrambledWord);
        textBox2.AppendText(sb.ToString());
    }
}

我的新课程是 ScrambleTextBoxText,我只是从 textBox1 中得到一个单词,随机地对其进行加扰,然后将加扰的单词添加回 textBox2

但是在 textBox2 中,我看到一个长字符串中的所有单词,例如:

dannyhihellobyethis

单词之间根本没有空格。我需要将它添加到 textBox2 中,并使用它在 textBox1 中的确切空格。

如果在 textBox1 中是例如:

丹尼你好嗨是的二四

moses daniel    yellow

所以在 textBox2 它应该是同一行:

丹尼你好嗨是的二四

moses daniel    yellow

有相同的空间,两行向下和一切。

两个问题:

  1. textBox2 中没有空格

  2. 它将我在 textBox1 中输入的任何单词添加到 textBox2 但它应该只添加从我的新类返回的单词:scrmbltb.scrambledWord

例如,如果我在 textBox1 中输入:hi daniel

所以在 textBox2 中应该是 : daniel 没有这个词 : hi

或者如果在 textBox1 中是:daniel hi hello 那么在 textBox2 中它将是:daniel hello

4

5 回答 5

4

为什么不将它们分开并单独使用呢?例如:

StringBuilder sb = new StringBuilder();
var words = textBox1.Text.Split(new char[] { ' ' });
foreach (var w in words)
{
    if (string.IsNullOrEmpty(w))
    {
        sb.Append(w);
        continue;
    }

    // do something with w
    sb.Append(w);
}

该算法将保留所有空格,但允许您w在添加之前进行操作。

于 2013-07-02T12:41:58.973 回答
1

快速简单:

string text = textBox1.Text;

string[] words = text.Split(new string[] { }, StringSplitOptions.RemoveEmptyEntries);

foreach (string word in words)
{
    textBox2.Text += " " + ChangeWord(word);
}

如果您不喜欢前导空格:

textBox2.Text = textBox2.Text.Trim();

编辑

我刚刚注意到您也想更改 ad-hoc 的词。在这种情况下,请参阅上面的更改并添加以下内容:

private string ChangeWord(string word)
{
    // Do something to the word
    return word;
}
于 2013-07-02T12:46:40.977 回答
1
var str = textbox1.Text.split(' ');
string[] ignoreChars = new string[] { ",", "." };

foreach(string t in str)
{
   if(!ignoreChars.Contains(t)) //by this way, we are skipping the stuff you want to do to the words
   {
     if(!int.TryParse(t)) // same here
     {
         //dosomething to t
         // t = t + "asd";
     }
   }
   textBox2.Text += " " + t;
}
于 2013-07-02T12:49:08.597 回答
0

尝试执行以下操作:

String str=TextBox1.Text;
String[] tokens = str.split(" ");

for(int i=0;i<tokens.length();i++)
{
  String retVal = tokens[i];
}

TextBox2.Text=retVal;
于 2013-07-02T12:43:33.993 回答
0

您可以对 C# 使用 getline 或 readline ,这将在文本框中获取整行,然后将其存储在临时变量中。

于 2013-07-02T12:47:55.243 回答