将NSString拆分为Objective-C中的数组 [英] Split an NSString into an array in Objective-C

查看:102
本文介绍了将NSString拆分为Objective-C中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串@Hello分割为数组item0:'H',item1:'e',item2:'l',item3:'l',item4:'o'。我在所有地方搜索,但不能找到如何..:(



哦,当然在Objective-C。



感谢您的帮助。

解决方案

如果您对 char s,try:

  const char * array = [@HelloUTF8String]; 



如果需要NSArray,请尝试:

  NSMutableArray * array = [NSMutableArray array]; 
NSString * str = @Hello;
for(int i = 0; i <[str length]; i ++)
NSString * ch = [str substringWithRange:NSMakeRange(i,1)];
[array addObject:ch];
}

并且数组将包含每个字符作为它的元素。


how can I split the string @"Hello" to an array item0:'H', item1:'e', item2:'l', item3:'l', item4:'o'. I searched everywhere but cant find how.. :(

oh, in Objective-C of course.

thanks for your help.

解决方案

If you're satisfied with a C array of chars, try:

const char *array = [@"Hello" UTF8String];

If you need an NSArray, try:

NSMutableArray *array = [NSMutableArray array];
NSString *str = @"Hello";
for (int i = 0; i < [str length]; i++) {
    NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
    [array addObject:ch];
}

And array will contain each character as an element of it.

这篇关于将NSString拆分为Objective-C中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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