What is the best way to analyze a string?
I have a string that contains three words. Using if statements, I want the words to have different meanings in different contexts.
In the order each happens, I want the method that analyze the string to do the following:
1. Verify that the string is in the required format (two or three words, if two then the first has only numbers, if three then the two first words have only letters (To prevent bugs. Can use catch blocks instead)).
2. Store the last word in another string, and then delete it from the original string.
3. Store the first word in another string, then check if the string has two (false) or three (true) words.
4. If the verifying process returns true, verify something using the first word, and then delete said word from the original string. Then run the original string through if statements and get desired result.
4. If the verifying process returns false, store the original string in another string from another class, using the string with the last word to tell which string.
John covered quite a lot of the useful methods; but in your case I'd also look at String.split(). Assuming that your "words" are whitespace-delimited, the usual form is:
String words = myString.split("\\s+");
and once you have an array of words, the rest should be plain sailing.