iphone - Read UTF8 character in specify position from a NSString -


      nsstring* str = @"1二3四5";     nslog(@"%c",[str characteratindex:0]);      nslog(@"%c",[str characteratindex:1]);    

nsstring - characteratindex works on ascii chars, how utf8 character @ index of 2?

-- updated --
seems unichar(16bits) can't represent utf8 encoding strings(8bites 32bites), there method char nsstring?

unfortunately dave's answer doesn't want. index supplied rangeofcomposedcharactersequenceatindex index of utf-16 code unit, 1 or 2 or make utf-16 code point. 1 not second utf-16 code point if first code point in string requires 2 code units... (rangeofcomposedcharactersequenceatindex returns range of code point includes code unit @ given index, if first char requires 2 code units passing index of 0 or 1 returns same range).

if want find utf-8 sequence character can use utf8string , parse resultant bytes find byte sequence nth character. or can likewise use rangeofcomposedcharactersequenceatindex starting @ index 0 , iterate till nth character, convert 1 or 2 utf-16 code units utf-8 code units.

i hope we're missing , built-in...

a start (needs bounds checking!) of category might help:

@interface nsstring (utf)  - (nsrange) rangeofutfcodepoint:(nsuinteger)number;  @end  @implementation nsstring (utf)  - (nsrange) rangeofutfcodepoint:(nsuinteger)number {     nsuinteger codeunit = 0;     nsrange result;     for(nsuinteger ix = 0; ix <= number; ix++)     {         result = [self rangeofcomposedcharactersequenceatindex:codeunit];         codeunit += result.length;     }     return result; }  @end 

but sort of stuff more efficient using char * rather nsstring


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -