Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:04
Show Gist options
  • Save Dynyx/2868240 to your computer and use it in GitHub Desktop.
Save Dynyx/2868240 to your computer and use it in GitHub Desktop.
Convert a string to a List of words
private static List<string> stringToWordArray(string s)
{
List<string> words = new List<string>();
string word;
s = s.ToUpper();
s = s.Trim(',', '.', '"', '-', ' ', ':', ';', '\n', '!', '?');
while (s.Length > 0)
{
if (s.Contains(' '))
{
word = s.Substring(0, s.IndexOf(" "));
s = s.Substring(s.IndexOf(" ") + 1);
s = s.Trim();
}
else
{
word = s;
s = "";
}
word = word.Trim(',', '.', '"', '-', ' ', ':', ';', '\n', '!', '?');
if (word.Length > 0)
words.Add(word);
else
return words;
}
return words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment