Detecting whitespace in textbox

瘦欲@ 提交于 2019-12-01 14:40:35

use IndexOf

if( "1 1a".IndexOf(' ') >= 0 ) {
    // there is a space.
}

This function should do the trick for you.

bool DoesContainsWhitespace()
{
   return textbox1.Text.Contains(" ");
}
int NumberOfWhiteSpaceOccurances(string textFromTextBox){
 char[] textHolder = textFromTextBox.toCharArray();
 int numberOfWhiteSpaceOccurances = 0;
 for(int index= 0; index < textHolder.length; index++){
   if(textHolder[index] == ' ')numberOfWhiteSpaceOccurances++;
 }
 return numberOfWhiteSpaceOccurances;
}

Not pretty clear what the problem is, but in case you just want a way to tell if there is a white space anywhere in a given string, a different solution to the ones proposed by the other stack users (which also work) is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Testing
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(PatternFound("1 1 a"));
            Console.WriteLine(PatternFound("1     1    a"));
            Console.WriteLine(PatternFound("            1     1   a"));

        }

        static bool PatternFound(string str)
        {
            Regex regEx = new Regex("\\s");
            Match match = regEx.Match(str);
            return match.Success;
        }
    }
}

in case what you want is determining whether a given succession of consecutive white spaces appear, then you will need to add more to the regex pattern string. Refer to http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx for the options.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!