I don’t have much experience in objective-c, sorry if this is really obvious.
What I need is to split a NSString into tokens. The tokens are separated by spaces or by another sign (not a letter). The catch is that I want to keep the separators except when they are spaces.
Example phrase: “a b c,d’s, e f.” from this I would like to get:
"a"
"b"
"c"
","
"d"
"'"
"s"
","
"e"
"f"
"."
With this code:
NSMutableCharacterSet *separators = [NSMutableCharacterSet punctuationCharacterSet];
[separators formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *parse_array = [intext componentsSeparatedByCharactersInSet:separators];
I get only the letters. If I just filter the whitespaces and NL I get the signs together with the letters. What i need is to perform two parsings in sequence (first the whitespaces and Nl and then the punctuation), but I really don’t know how to do it in objective-c. Can anyone give me a hint?
Thanks!
Well, you could do something like this to remove all the whitespace from a string:
Then you could just iterate over the characters and turn them into
NSStrings:Or if you didn’t want to strip the whitespace before, you could do it in the loop: