简体   繁体   English

如何检查textBox内容中是否有空格?

[英]How do i check if there is spaces in textBox content?

I have this code : 我有这个代码:

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());
        }

For example in textBox1 i did typed pressed the space bar key 7 times and then typed some words and then 5 spaces again and a word: 例如,在textBox1我确实键入了按空格键7次,然后键入了一些单词,然后又键入了5个空格和一个单词:

danny hi hello daniel     hello

So lets say danny is after 7 spaces from the beginning in textBox1 and between daniel and hello there are more 5 spaces. 因此,可以说danny在textBox1的开头起7个空格,而daniel和hello之间还有5个空格。

In my code i did: 在我的代码中,我做到了:

if (string.IsNullOrEmpty(words[i]))
{
    sb.Append(words[i]);
}

But that never will happen and its not right. 但这永远不会发生,这是不对的。 I wanted to check that if before or after a word in the textBox there is any space/s add the space/s to the sb variable. 我想检查一下是否在textBox中的单词之前或之后是否有空格,将空格添加到sb变量中。

So in the end textBox2 content will be the same as in textBox1 with the same number of spaces between the words. 因此,最终textBox2内容将与textBox1中的相同,并且单词之间的空格数相同。

Now textBox2 looks like a long one string of words without any spaces between them. 现在, textBox2看起来像一个长长的字符串,它们之间没有任何空格。

My problem is how to add the same spaces between the words from textBox1 ? 我的问题是如何在textBox1的单词之间添加相同的空格?

I simplified your code a little, but you should find it easy to apply in your situation. 我对代码进行了一些简化,但是您应该发现它很容易应用于您的情况。 The problem comes from the fact that you are losing the spaces when you do the split and they are not being added back in. The solution is to use "String.Join" when you have the finished collection of strings. 问题来自于这样一个事实,即您在进行拆分时会丢失空格,而不会重新添加空格。解决方案是在完成字符串收集后使用“ String.Join”。 In this case since you know the output size is the same as the input size, I don't see any reason to use the stringbuilder. 在这种情况下,由于您知道输出大小与输入大小相同,因此我看不出使用stringbuilder的任何理由。 Just use an array you size to the input. 只需使用您要调整大小的数组作为输入即可。

string inputText = "This is a test";

        var words = inputText.Split(new char[] { ' ' });
        var outputWords = new string[words.Length];
        for (int i = 0; i < words.Length; i++)
        {
            if (string.IsNullOrEmpty(words[i]))
            {
                outputWords[i] = words[i];
            }
            else
            {
                outputWords[i] = Scramble(words[i]);
            }
        }

        string outputText = string.Join(" ",outputWords);

This statement is absolutely useless: 该语句绝对没有用:

if (string.IsNullOrEmpty(words[i]))
{
    sb.Append(words[i]);
}

It seems you need something like this (not tested): 似乎您需要类似以下内容(未经测试):

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

    StringBuilder sb = new StringBuilder();
    var words = Regex.Split(textBox1.Text, @"(?=(?<=[^\s])\s+)");
    foreach (string word  in words)
    {
        ScrambleTextBoxText scrmbltb = new ScrambleTextBoxText(word.Trim());
        scrmbltb.GetText();
        sb.Append(word.Replace(word.Trim(), scrmbltb.scrambledWord));
    }
    textBox2.AppendText(sb.ToString());
}

Regex.Split(textBox1.Text, @"(?=(?<=[^\\s])\\s+)") splits the input string with preserving spaces. Regex.Split(textBox1.Text, @"(?=(?<=[^\\s])\\s+)")分割输入字符串并保留空格。

This the easy form 这是简单的形式

string text=mytextbox.Text;
while(text.Contains("  ")) //while two spaces
  text=text.Replace("  "," "); //remove two spaces

If i got it right, your problem is to keep the exact number of spaces between the then scrambled words. 如果我做对了,您的问题是在随后加扰的单词之间保留准确的空格数。

var words = string.Split(new char[]{' '}, StringSplitOptions.None); // this keeps the spaces as "epmty words"
var scrambled = words.Select(w => { if (String.IsNullOrEmpty(w))
                                          return w;
                                    else {
                                         ScrambleTextBoxText scrmbltb = new ScrambleTextBoxText(w);
                                         scrmbltb.GetText();
                                         return scrmbltb.scrambledWord;
                                    }
                                  });
var result = string.Join(" ", scrambled);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM