我有这个代码:
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[] { ' ' });
for (int i = 0; i < words.Length; i++)
{
if (string.IsNullOrEmpty(words[i]))
{
sb.Append(words[i]);
}
else
{
ScrambleTextBoxText scrmbltb = new ScrambleTextBoxText(words[i]);
scrmbltb.GetText();
sb.Append(scrmbltb.scrambledWord);
}
}
textBox2.AppendText(sb.ToString());
}
例如,在textBox1
我确实键入了按空格键 7 次,然后键入了一些单词,然后再次键入了 5 个空格和一个单词:
danny hi hello daniel hello
因此,可以说 danny 从一开始就在 7 个空格之后textBox1
,在 daniel 和 hello 之间还有 5 个空格。
在我的代码中,我做了:
if (string.IsNullOrEmpty(words[i]))
{
sb.Append(words[i]);
}
但这永远不会发生,而且是不对的。我想检查是否在文本框中的单词之前或之后有任何空格/s 将空格/s 添加到 sb 变量。
所以最终textBox2
内容将textBox1
与单词之间的空格数相同。
现在textBox2
看起来像一长串单词,它们之间没有任何空格。
我的问题是如何在单词之间添加相同的空格textBox1
?