Read UTF8 character in specify position from a NSString

前端 未结 3 633
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 13:12

    NSString* str = @\"1二3四5\";
    NSLog(@\"%c\",[str characterAtIndex:0]); 
    NSLog(@\"%c\",[str characterAtIndex:1]);  

NSString - characterAtIndex works we

3条回答
  •  暖寄归人
    2021-02-03 13:37

    You'd use the more verbose methods:

    NSRange rangeOfSecondCharacter = [str rangeOfComposedCharacterSequenceAtIndex:1];
    NSString *secondCharacter = [str substringWithRange:rangeOfSecondCharacter];
    

    ...with proper bounds and range checking, of course. Note that this gives you an NSString, an object, not a unichar or some other primitive data type.

提交回复
热议问题