strings - determining if a string has more than one word?

Hi all,

I have a need where I am required to determine whether a string contains more than one word. If the string contains one word I don't want to do anything, but if it conatians more than one word, I need to do something.

Whats the best way for me to tackle this

Thanks

Tryst



Answer this question

strings - determining if a string has more than one word?

  • madog5353

    using regular expressions ;)

    public System.Collections.Generic.List<string> GetWords(string Input)

    {

    string[] Result = System.Text.RegularExpressions.RegEx.Split(Input, @"\b");

    System.Collections.Generic.List<string> Words = new System.Collections.Generic.List<string>();

    foreach (string s in Result)

    {

    if (System.Text.RegularExpressions.RegEx.IsMatch(s, @"[a-z]+", Text.RegularExpressions.RegexOptions.IgnoreCase))

    {

    Words.Add(s);

    }

    }

    return Words;

    }

    FYI: the \b pattern represents word boundaries. I split the string by word boundaries and then i only return the strings that have pure letters in them ;)

  • darkangel_17

    Tryst,
    Unless you are looking for something more sophisticated (e.g. making sure your words are meaningful matching them against some dictionary), you could proceed as follows:

    since a word is a sequence of characters, separated from subsequent words by spaces and other separators, you need to:

    1) make sure there are no extra separators, at the head or tail of your string.

    2) see if the string contains any of those separators.

    For instance:

    char [] sep = new char [] { ' ', '\t', ',', '.', ':' }; // complete this list as you see fit
    string myString = "the string I want to test";

    bool isSingleWord = myString.Trim (sep).IndexOfAny (sep) < 0;

    Alternatively, you may use a regular expression.

    bool isSingleWord = ! System.Text.RegularExpressions.Regex.IsMatch (myString, "\w+\W+\w");

    this means: match a sequence of one or more word characters, followed by one or more non-word character, followed by one or more word characters. If this pattern can be found anywhere in your string, the IsMatch returns true. You can tweak the match string to your liking, looking at the documentation of regular expressions.

    HTH
    --mc


  • tanitoUy

    You're missing words that are seperated by "-" or any other symbol with that method

  • Dan_Icon

    Another one liner (assuming words are separated by spaces as in the normal English language)...

    return text.IndexOf(' ');

    returns -1 if it contains a single word, returns 0 or greater if there are multiple words.

    But just to be safe you need to do a 'text.Trim()' before that line to get rid of any possible spaces from start and end of the string!



  • LoisW

    A dirty method:

    private bool ContainsMultipleWords(string String)

    {

    string newstring = String.Replace(" ", null);

    if (newstring.Length == String.Length)

    {

    return false;

    }

    else

    {

    return true;

    }

    }

    private void Form1_Load(object sender, EventArgs e)

    {

    Console.WriteLine(ContainsMultipleWords("one two"));

    }



  • Shash58691

    private bool IsSingleWord(string text)

    {

    return (text.split(",.;: ".ToCharArray()).Length > 1);

    }



  • strings - determining if a string has more than one word?