2015-11-20 22 views
6

Sto cercando di ottenere singoli caratteri da NSString, come "storico", "Release", "rabbia". Voglio output come 1) AI, modello, sì, Ce, o 2) Q, il, inizio, 3) Corvo, Il, ma la produzione sta venendo simili 1) AI, g, b, t, f , 2A) il parametro, cioè, r, h, installazione, messa in servizio, 3) una, vale a dire, r, s, il codicecome ottenere singoli caratteri da stringa a Ios per la lingua Gujrati (altra lingua)

ho usato come di seguito:

NSMutableArray *array = [[NSMutableArray alloc]init]; 

    for (int i=0; i<strElement.length; i++) 
    { 
       NSString *str = [strElement substringWithRange:NSMakeRange(i, 1)]; 
       [array addObject:str]; 

    } 
    NSLog(@"%@",array); 

Diamo strElement come "rabbia", poi ho avuto output come questo ક , ્ , ર , ો , ધ Ma ho bisogno di output come questo ક્રો,ધ

C'è un modo che io possa ottenere il risultato desiderato? Qualsiasi metodo disponibile direttamente in iOS o deve essere creata anche con la mia auto quindi alcun modo o idea di come crearlo?

Qualsiasi aiuto è apprezzato

risposta

6

Il codice è supponendo che ciascun carattere della stringa è un singolo valore unichar. Ma non lo è. Alcuni dei caratteri Unicode sono composti da più unichar valori.

La soluzione è quella di utilizzare rangeOfComposedCharacterSequenceAtIndex: anziché substringWithRange: con una lunghezza fissa di gamma 1.

NSString *strElement = @"ઐતિહાસિક પ્રકાશન ક્રોધ"; 
NSMutableArray *array = [[NSMutableArray alloc]init]; 

NSInteger i = 0; 
while (i < strElement.length) { 
    NSRange range = [strElement rangeOfComposedCharacterSequenceAtIndex:i]; 
    NSString *str = [strElement substringWithRange:range]; 
    [array addObject:str]; 
    i = range.location + range.length; 
} 

// Log the results. Build the results into a mutable string to avoid 
// the ugly Unicode escapes shown by simply logging the array. 
NSMutableString *res = [NSMutableString string]; 
for (NSString *str in array) { 
    if (res.length) { 
     [res appendString:@", "]; 
    } 
    [res appendString:str]; 
} 
NSLog(@"Results: %@", res); 

This uscite:

Risultati: AI, modello, sì, Ce, o, Q, il, parole, non, Crow, i

+0

grazie @rmaddy questo lavoro come un fascino –