When using stringByReplacingOccurrencesOfString, it seems to replace words inside words. So for example,
The house was held together by...
replacing occurrences of ‘the’ with ‘A’ will result in
A house was held togeAr by...
How can I avoid this? I know I could add whitespace on either side of the word being replaced to ensure it’s not part of a longer word, however this doesn’t work in all cases, specifically where the word being replaced is the first or last word in the sentence (that is, when white space isn’t on both sides).
You should use an
NSRegularExpressionwith the pattern\bthe\bwhere\bindicates a word boundary.