1

I would like to get only the first character, symbol, number, or emoji of an NSString.

Since emojis are encoded as unicode surrogate pairs, just getting the first character using something like

str = [str substringToIndex:1];

will not work.

0

1 Answer 1

2

Following an answer found here: Simplest way to extract first Unicode codepoint of an NSString (outside the BMP)?

NSRange r = [myString rangeOfComposedCharacterSequenceAtIndex:0];
NSString *firstCharacter = [myString substringWithRange:r];

firstCharacter gives you the first character of the NSString no matter how it's encoded.

Not the answer you're looking for? Browse other questions tagged or ask your own question.