2015-11-20 36 views
6

Próbuję dostać pojedyncze znaki z NSString, jak „historyczne”, „Release”, „gniew”. I chce wyjściowy jak 1) AI, wzór tak, Ce, albo 2) Q, jest, uruchomić, 3) Crow The, a wyjście zbliża się ten 1) AI, G, B, T, F 2A) parametr, czyli R, h, montaż, uruchomienie, 3), to znaczy, R, s, przyjak dostać pojedyncze znaki z łańcucha na iOS dla języka Gujrati (inny język)

Użyłem kodu jak poniżej:

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); 

Weźmy strElement jako „gniew”, to mam wyjścia jak ten ક , ્ , ર , ો , ધ Ale muszę wyjście jak ten ક્રો,ધ

Czy jest jakiś sposób, że można uzyskać pożądany wynik? Każda metoda dostępna bezpośrednio w iOS lub trzeba utworzyć go przez mojego ja wtedy jakikolwiek sposób lub pomysł jak go stworzyć?

Każda pomoc jest mile widziana

Odpowiedz

6

Kod jest przy założeniu, że każdy znak w ciągu jest pojedyncza wartość unichar. Ale tak nie jest. Niektóre ze znaków Unicode składają się z wielu wartości unichar.

Rozwiązaniem jest użycie rangeOfComposedCharacterSequenceAtIndex: zamiast substringWithRange: o stałej długości w zakresie 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); 

tej wyjść:

Wyniki: AI, wzór, tak, CE, lub Q, jest, słowa, nie, Crow, że

+0

dzięki @rmaddy tej pracy jak urok –