How do I split a string with special characters into a NSMutableArray

青春壹個敷衍的年華 提交于 2020-01-02 05:14:12

问题


I'am trying to seperate a string with danish characters into a NSMutableArray. But something is not working. :(

My code:

NSString *danishString = @"æøå";

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[danishString length]]; 

for (int i=0; i < [danishString length]; i++) 
{ 
     NSString *ichar = [NSString stringWithFormat:@"%c", [danishString characterAtIndex:i ]]; 
     [characters addObject:ichar]; 
} 

If I do at NSLog on the danishString it works (returns æøå);

But if I do a NSLog on the characters (the array) I get some very stange characters - What is wrong?

/Morten


回答1:


First of all, your code is incorrect. characterAtIndex returns unichar, so you should use @"%C"(uppercase) as the format specifier.

Even with the correct format specifier, your code is unsafe, and strictly speaking, still incorrect, because not all unicode characters can be represented by a single unichar. You should always handle unicode strings per substring:

It's common to think of a string as a sequence of characters, but when working with NSString objects, or with Unicode strings in general, in most cases it is better to deal with substrings rather than with individual characters. The reason for this is that what the user perceives as a character in text may in many cases be represented by multiple characters in the string.

You should definitely read String Programming Guide.

Finally, the correct code for you:

NSString *danishString = @"æøå";
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[danishString length]]; 
[danishString enumerateSubstringsInRange:NSMakeRange(0, danishString.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    [characters addObject:substring];
}];

If with NSLog(@"%@", characters); you see "strange character" of the form "\Uxxxx", that's correct. It's the default stringification behavior of NSArray by description method. You can print these unicode characters one by one if you want to see the "normal characters":

for (NSString *c in characters) {
    NSLog(@"%@", c);
}



回答2:


In your example, ichar isn't type of NSString, but unichar. If you want NSStrings try getting a substring instead :

NSString *danishString = @"æøå";
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[danishString length]]; 

for (int i=0; i < [danishString length]; i++) 
{ 
    NSRange r = NSMakeRange(i, 1);
    NSString *ichar = [danishString substringWithRange:r]; 
    [characters addObject:ichar]; 
}



回答3:


You could do something like the following, which should be fine with Danish characters, but would break down if you have decomposed characters. I suggest reading the String Programming Guide for more information.

NSString *danishString = @"æøå";
NSMutableArray* characters = [NSMutableArray array];
for( int i = 0; i < [danishString length]; i++ ) {
  NSString* subchar = [danishString substringWithRange:NSMakeRange(i, 1)];
  if( subchar ) [characters addObject:subchar];
}

That would split the string into an array of individual characters, assuming that all the code points were composed characters.




回答4:


It is printing the unicode of the characters. Anyhow, you can use the unicode (with \u) anywhere.



来源:https://stackoverflow.com/questions/8740274/how-do-i-split-a-string-with-special-characters-into-a-nsmutablearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!