当我尝试提取一个字符串:"hello hihi wold hehe“to array {"hello ","hihi","wold","hehe","hehe"}
-(void)getString:(NSString *)emution
{
int n=[emution length];
int x=0,y=0,i=0;
char temp;
while (true) {
temp=[emution characterAtIndex:i];
if(temp == '[')
{
x=i;
}else
{
if(temp==']')
{
y=i+1;
NSLog(@"x: %d , y:%d",x,y);
NSString *sub=[emution substringWithRange:NSMakeRange(x, y)];
NSLog(@"here:%@",sub);
x=y;
sub=nil;
}
if(i>=n-1)
{break;}
}
i++;
}
}结果这不是真的,我不确定,但当我尝试用java编写代码时,它是真的。
发布于 2013-11-19 11:24:03
我想你想要的是:
NSArray *array = [emution componentsSeparatedByString:@" "];另外,我要指出的是,结束循环if(i>=n-1)的检查应该在递增i之后进行,而不是在else语句中。
https://stackoverflow.com/questions/20062413
复制相似问题