gpt4 book ai didi

iphone - 将前面带有标签的 NSString 中的单词分开

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

我有一个 NSString,例如:

"Had a #great time at the #party last night."

我想把它分成一个数组,这样:

"Had a "
"#great"
" time at the "
"#party"
" last night."

我该怎么做?

最佳答案

NSString *str = @"Had a #great time at the #party last night.";

NSMutableArray *arr = [[NSMutableArray alloc] init];
NSArray *array = [str componentsSeparatedByString:@"#"];
NSMutableString *retStr= [[NSMutableString alloc] initWithString:[array objectAtIndex:0]];
[arr addObject:retStr];
for(int i=1 ; i<[array count];i++)
{
NSArray *array1 = [[array objectAtIndex:i] componentsSeparatedByString:@" "];
{
NSMutableString *retStr= [[NSMutableString alloc]init];
for (int i = 0;i< [array1 count]; i++)
{
if(i==0)
{
[retStr appendFormat:@" #%@ ",[array1 objectAtIndex:i]];
[arr addObject:retStr];
retStr= [[NSMutableString alloc]init];
}
else
{
[retStr appendFormat:@"%@ ",[array1 objectAtIndex:i]];
}
}
[arr addObject:retStr];
}
}
NSLog(@"%@",arr);

你会得到你想要的正确输出

关于iphone - 将前面带有标签的 NSString 中的单词分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16278040/

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