Hi
I have a textbox which contains Text.
I want to find all the words in the text. (which seprate from each other with space).
what should I do?
thx for ur attention.

Well, you probably want to remove any leading or trailing spaces so you don't count extra words--see string.Trim() . Then, you could loop through the string looking for " " (space) and count each one you find using string.IndexOf() or IndexOfAny() , but you probably want another loop inside of that to skip extra spaces so you only count one word for mulitple contiguous spaces e.g. " " (two spaces together).

If you know your string contains only words separated by spaces you could use:

string Mystr = "This is a sentence.";
string[] split = Mystr.Split(new Char[] {' '});

Now split[1] would contain the word "is".

If you know your string contains only words separated by spaces you could use:

string Mystr = "This is a sentence.";
string[] split = Mystr.Split(new Char[] {' '});

Now split[1] would contain the word "is".

Ah, yes! then just iterate through the array and count only those have Length > 0 --nice!

thx.It was really intersting.
but lokk at this sentence:"This is Daniweb Forums.Good Luck."
'Good' is word too.but we split using ' ' not '.'
It's the same about '!','@',...
what about now?

>>I want to find all the words in the text. (which seprate from each other with space).
You changed the rules

But to answer your question you can use RegEx:

private void button15_Click(object sender, EventArgs e)
    {
      const string sentence = @"This is Daniweb Forums.Good Luck";
      string[] regex = System.Text.RegularExpressions.Regex.Split(sentence, @"\W");
      System.Diagnostics.Debugger.Break();
    }

Results in:

regex
{string[6]}
    [0]: "This"
    [1]: "is"
    [2]: "Daniweb"
    [3]: "Forums"
    [4]: "Good"
    [5]: "Luck"
commented: this guy knows everything! +1

But to answer your question you can use RegEx:

private void button15_Click(object sender, EventArgs e)
    {
      const string sentence = @"This is Daniweb Forums.Good Luck";
      string[] regex = System.Text.RegularExpressions.Regex.Split(sentence, @"\W");
      System.Diagnostics.Debugger.Break();
    }

You lost me.... I tested it and the "\\W" works fine, but what is it?

It is a regex match pattern for word boundary. Take a look at "regular expressions" online or in the help file. There are a lot of expressions you can use

I heard about it but I didn't try it ever.It's very interesting.
but when I test it , I had an error on this line:

System.Diagnostics.Debugger.Break();

and another question(maybe stupid question):
where the words will be shown?(on textbox or messagebox or ...)
thx.

Dajer,

Comment out the line System.Diagnostics.Debugger.Break(); . All that line does is make the debugger break. I use that so I can test code and evaluate that it worked properly before I paste it up here :P

The words will not be shown as it currently stands. How do you want them shown? Your original post said you just wanted to find them....

private void button15_Click(object sender, EventArgs e)
    {
      const string joinChar = @"|";
      const string sentence = @"This is Daniweb Forums.Good Luck";
      string[] words = System.Text.RegularExpressions.Regex.Split(sentence, @"\W");
      MessageBox.Show("The following words were found (not including the pipe in between them): " + Environment.NewLine +
        joinChar + string.Join(joinChar, words) + joinChar);
    }

Or more simply

private void button15_Click(object sender, EventArgs e)
    {
      const string sentence = @"This is Daniweb Forums.Good Luck";
      string[] words = System.Text.RegularExpressions.Regex.Split(sentence, @"\W");
      for (int i1 = 0; i1 < words.Length; i1++)
        MessageBox.Show(string.Format("Word #{0:F0}: {1}", i1, words[i1]));
    }
private void button15_Click(object sender, EventArgs e)
    {
      const string sentence = @"This is Daniweb Forums.Good Luck";
      string[] words = System.Text.RegularExpressions.Regex.Split(sentence, @"\W");
      foreach (string word in words)
        MessageBox.Show(word);
    }

I hope that answers your question on how to display the words.


I hope that answers your question on how to display the words.

Thx alot.I think I should start reading Regex.

You're welcome

Please mark this thread as solved if you have found an answer to your problem and good luck!

Although regex would be the way to go here, I just want to point out that this would also work:

Mystr=@"This is Daniweb Forums.Good Luck" ;
string[] split = Mystr.Split(new Char[] {' ','.'});
commented: good follow up +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.