I've been trying to replace a substring of an NSString and it has been driving me crazy - the problem is the edge case where characters in the string take more than one UTF-16 codepoints, like Emoji. characterAtIndex: is kind of useless in this case and my best solution was going character by character getting the ranges for UTF-chars but my code seems to crash with out-of-range exceptions. Could anyone lend me a second pair of eyes? Code is at http://pastebin.com/kazBx6Qt