如何在没有分隔符的情况下分割单词?

时间:2018-07-05 14:04:15

标签: ios objective-c

我需要输入“ h”,“ e”,“ l”,“ l”,“ o”,但仍为“ hello”。

NSString* s = @"hello";
NSArray* arr = [s componentsSeparatedByString:@""];

2 个答案:

答案 0 :(得分:4)

使用[NSString UTF8String]

简单

这是NSString中的属性:

@property(readonly) const char *UTF8String;

您获得了这个char指针,然后将其用作char数组。

NSString *s = @"hello";
char *sArray = [s UTF8String];

for (int i = 0; i < [s length]; i++) {
    printf("%c\n", sArray[i]);
}

答案 1 :(得分:3)

componentsSeparatedByString并非如此。这是来自Apple的示例:

NSString *list = @"Karin, Carrie, David";
NSArray *listItems = [list componentsSeparatedByString:@", "];

我可能会尝试使用characterAtIndex循环遍历并一次添加到可变数组中。