I have the following string:
"SIT SIT UP SIT UP AND EAT AND EAT CHARLIE PAPA SIT UP AND EAT FOUR JUMP IN THE AIR FOR WATER FIVE SECONDS"
I also have an array containing valid string structures. Here's an example
NSArray *validphrases = @["SIT UP",
"SIT UP AND EAT",
"CHARLIE",
"PAPA",
"JUMP IN THE AIR FOR",
"FOUR",
"FIVE",
"SECONDS",
"JUMP IN THE WATER FOR" nil];
/** And the end result should be these valid phrases detected in order:
1. Sit up
2. Sit up and eat
3. Charlie
4. Papa
5. Sit up and eat
6. Four
7. Jump in the air for
8. Five
9. Seconds */
If multiple strings match at a given point in the string, it should return the longest one, and then skip past it.
Whats the quickest way that I can find the validPhrases string patterns from the string provided omitting any other words that don't satisfy the validPhrase patterns
What this will do is filter the string taking all the extra words away leaving only validPhrases and put that in a new array called preCommands array My current implementation works like this, but there should be an easier way
Split string by components separated by the " " character.
Iterate through the components and find out if that current word:
- Is present as a strings structure in the validPhrases array: If so then save that word into a preCOMMANDS array.
- Else execute another array and continue checking the next word appending to the first word until the set of words matches a pattern from the validPhrases array
- When it does match, add phrase to the preCOMMANDS array
This method of mine only works when the phrases are side by side each other in the string, For example
"SIT UP AND EAT" //will work but
"SIT SIT UP AND EAT" //will not work if you follow the formula
Is there an Objective-C method function that I can use to grab all phrases from a string in one go? I feel like I'm reinventing the wheel for something that I'm sure is possible with another method.