views:

58

answers:

2

Hey All,

I have a string full of a few hundred words.

How would I get each "word" (this can also be a single letter number or punctuation), and as each "word" is found, it is removed from the string.

Is this possible?

Example:

String:

"this is a string full of words and letters and also some punctuation! and num6er5."

As far as the algorithm is concerned, there are exactly 15 words in the above string.

+1  A: 

You need to define what are the non-word characters and then you can use String.Split() :

  string text = "the quick brown fox";
  string[] words = text.Split(' ', '.', ',', '\t', StringSplitOptions.RemoveEmptyEntries);
Henk Holterman
Thank you very much for the code sample. As bad as I feel right now, I marked Streamcap's post as the "answer" simply because you have tens of thousands more rep that he/she does. Sorry. Both of your answers were exactly what I was hoping for and since I often end up feeling conflicted about which answer to accept, I have made the decision to simply accept the person's answer who has a great deal less reputation, providing they are of equal value.
lucifer
+3  A: 

What you're trying to do is known as tokenizing.

In C#, the string Split() function works pretty well. If it's used like in Niedermair's code without any parameters, it returns an array of strings split (splitted?) by any spaces like this:

"I have spaces" -> {"I", "have", "spaces"}

You can also give any chars to split on as a parameter to Split() (for instance, ',' or ';' to handle csv files).

The Split() method pays no heed to what goes into the strings, so any letters, numbers and other chars will be handled.

About removing the words from the string: You might want to write the string into a buffer to achieve this, but I seriously think that's going too far. Strings are immutable which means any time you remove the "next word" you'll have to recreate the entire string object. It will be a lot easier to just Split() the entire string, throw the string away, and work with the array from there on.

Streamcap
Thanks for that Streamcap. :)
lucifer