gpt4 book ai didi

ios - subString emution ios

转载 作者:行者123 更新时间:2023-11-28 22:22:08 25 4
gpt4 key购买 nike

当我尝试提取字符串 :"hello [hihi] wold [hehe]"到数组 {"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 编写代码时,这是真的

最佳答案

我想你想要的是:

NSArray *array = [emution componentsSeparatedByString:@" "];

此外,我注意到结束循环 if(i>=n-1) 的检查应该在您递增 i 之后,而不是在 else 语句中.

关于ios - subString emution ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com