Ok lets suppose i've got this string:
R RATS ARE FAR
I want to replace the independent R with an X so to make:
X RATS ARE FAR
I've tried stringByReplacingOccurrencesOfString but that replaces all R's to X's. One way to accomplish it is to check if there's a space before and after the letter, which would mean its independent and then go for the kill, but this approach seems so lame. Also the string is dynamic and so are the position of R's.
I'm assuming here that words can be surrounded by just about any non-alphanumeric character, right? (Otherwise you'd just replace @" R " with " X ", plus special cases for leading/trailing occurrences)
One way to do it would be to use RegexKitLite's
(
\b
denotes word *b*oundaries in PCRE-style regular expressions)And then there is CFStringTokenizer (search linked documentation for
kCFStringTokenizerUnitWordBoundary
), which might be of help, alternatively.