我有一个类似这样的东西:
NSString *str = "hello this is 123.";
我希望将其放入字符数组中,以便我可以逐个字符地读取它。
因此,它将像这样:
charArray[0] = 'h'
charArray[1] = 'e'
charArray[2] = 'l'
如何将字符串转换为字符数组并且如何读取字符数组中的每个单元格?
我有一个类似这样的东西:
NSString *str = "hello this is 123.";
charArray[0] = 'h'
charArray[1] = 'e'
charArray[2] = 'l'
尝试像这样做,注意它是大写的 C,这会在特殊字符方面给你带来麻烦。
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < [string length]; i++) {
[array addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]];
}
NSString *s = @"hello this is 123.";
const char *c = [s UTF8String];
NSString 可能包含无法适应 char 的 utf 8 或 utf 16 字符,因此访问底层 char 数组可能是一个不好的主意。
如果你想要的话,可以使用 characterAtIndex 消息来访问给定的字符并遍历字符串。