Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 26, 2006 4:05 PM
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
All replies (7)
Wednesday, April 26, 2006 4:22 PM ✅Answered | 1 vote
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
Wednesday, April 26, 2006 4:38 PM ✅Answered | 1 vote
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 ;)
Wednesday, April 26, 2006 6:28 PM
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"));
}
Wednesday, April 26, 2006 6:31 PM
You're missing words that are seperated by "-" or any other symbol with that method
Thursday, April 27, 2006 2:58 PM
private bool IsSingleWord(string text)
{
return (text.split(",.;: ".ToCharArray()).Length > 1);
}
Sunday, April 30, 2006 7:34 AM
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!
Wednesday, March 21, 2012 2:22 PM
private static bool ValidNome(string nome)
{
string[] newstring = nome.Split();
if (newstring.Length > 1 && newstring[1] != "")
{
return true;
}
else
{
return false;
}
}