反正很快打破了字为字符目标C数组? [英] Anyway to quickly break a word into an array of characters in objective c?

查看:120
本文介绍了反正很快打破了字为字符目标C数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:的NSString * strWord =英语;

结果的NSArray会是这样的:简体中文

The result NSArray would be something like: e n g l i s h

我觉得是可能做到这一点。

I think is possible to do it with

[NSString的componentSeparatedByCharacter:somecharacter],但我不知道性格是什么...

[NSString componentSeparatedByCharacter:somecharacter], but I don't know what that character is...

我写了这样的事情,提取的字符:

I wrote something like this to extract the character:

NSString *character;
for (int i=0; i<[strWord length]; i++) {
    character = [strWord substringWithRange:NSMakeRange(i, 1)];
}

感谢您的帮助!

推荐答案

如果你需要一一列举,并支持iOS 4.0或更高版本,可以使用

If you need to enumerate them and support iOS 4.0 or later, you could use

[strWord enumerateSubstringsInRange:NSMakeRange(0, [strWord length])
                            options:NSStringEnumerationByComposedCharacterSequences
                         usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
    // substring here contains each character in turn
}];

您也可以只使用字符 -getCharacters复制到缓冲区:范围:如果你实际上并不需要他们作为一个NSArray。或者你可以只遍历字符串(使用块的方法,或致电 -characterAtIndex:反复,或复制出整个缓冲区,并遍历了),并构建您自己的数组。

You could also just copy the characters into a buffer using -getCharacters:range: if you don't actually need them as an NSArray. Or you could just iterate over the string (using the block method, or by calling -characterAtIndex: repeatedly, or by copying out the entire buffer and iterating over that) and construct your own array.

这篇关于反正很快打破了字为字符目标C数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆