遍历NSString中所有字符的最有效方法 [英] Most efficient way to iterate over all the chars in an NSString
本文介绍了遍历NSString中所有字符的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
迭代NSString中所有字符的最佳方法是什么?您是否想遍历字符串的长度并使用该方法.
What's the best way to iterate over all the chars in an NSString? Would you want to loop over the length of the string and use the method.
[aNSString characterAtIndex:index];
还是您要使用基于NSString的char缓冲区?
or would you want to user a char buffer based on the NSString?
推荐答案
我肯定会先获得一个char缓冲区,然后对其进行迭代.
I would definitely get a char buffer first, then iterate over that.
NSString *someString = ...
unsigned int len = [someString length];
char buffer[len];
//This way:
strncpy(buffer, [someString UTF8String]);
//Or this way (preferred):
[someString getCharacters:buffer range:NSMakeRange(0, len)];
for(int i = 0; i < len; ++i) {
char current = buffer[i];
//do something with current...
}
这篇关于遍历NSString中所有字符的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文