gpt4 book ai didi

objective-c - 如何将具有特殊字符的字符串拆分为 NSMutableArray

转载 作者:可可西里 更新时间:2023-11-01 06:12:17 26 4
gpt4 key购买 nike

我正在尝试将带有丹麦语字符的字符串分隔到 NSMutableArray 中。但是有些东西不起作用。 :(

我的代码:

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];
}

如果我在 NSLog 上对 danishString 进行操作(返回 æøå);

但是如果我对字符(数组)执行 NSLog,我会得到一些非常奇怪的字符 - 有什么问题吗?

/莫腾

最佳答案

首先,您的代码不正确。 characterAtIndex 返回 unichar,因此您应该使用 @"%C"(大写)作为格式说明符。

即使使用正确的格式说明符,您的代码也是不安全的,严格来说,仍然是不正确的,因为并非所有 unicode 字符都可以由单个 unichar 表示。您应该始终处理每个子字符串的 unicode 字符串:

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.

你绝对应该阅读 String Programming Guide .

最后,给你正确的代码:

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];
}];

如果使用 NSLog(@"%@", characters);,您会看到“\Uxxxx”形式的“奇怪字符”,这是正确的。这是 NSArray 通过 description 方法的默认字符串化行为。如果你想看到“普通字符”,你可以一个一个地打印这些 un​​icode 字符:

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

关于objective-c - 如何将具有特殊字符的字符串拆分为 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8740274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com