0

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.

4
Contributors
12
Replies
13
Views
7 Years
Discussion Span
Last Post by ddanbe
Featured Replies
  • 1
    ddanbe 2,525   7 Years Ago

    If you know your string contains only words separated by spaces you could use: [CODE=c#]string Mystr = "This is a sentence."; string[] split = Mystr.Split(new Char[] {' '});[/CODE] Now split[1] would contain the word "is". Read More

  • 1
    sknake 1,622   7 Years Ago

    >>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: [code=csharp] private void button15_Click(object sender, EventArgs e) { const string sentence = @"This is Daniweb Forums.Good Luck"; string[] regex = … Read More

  • 1
    sknake 1,622   7 Years Ago

    Dajer, Comment out the line [icode]System.Diagnostics.Debugger.Break();[/icode]. 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 … Read More

  • 1
    ddanbe 2,525   7 Years Ago

    Although regex would be the way to go here, I just want to point out that this would also work: [CODE=c#]Mystr=@"This is Daniweb Forums.Good Luck" ; string[] split = Mystr.Split(new Char[] {' ','.'});[/CODE] Read More

0

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).

1

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".

0

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!

0

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?

1

>>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"
Votes + Comments
this guy knows everything!
0

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?

0

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

0

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.

1

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.

0


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

Thx alot.I think I should start reading Regex.

0

You're welcome

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

1

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[] {' ','.'});
Votes + Comments
good follow up
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.