如何从 Objective-C 中的 NSMutableString 获取逗号分隔值?

发布于 2024-08-21 10:15:22 字数 891 浏览 5 评论 0原文

我在 Objective-C 中发出 HTTP 请求,得到的回复是

200,8,"7 Infinite Loop,库比蒂诺,CA 95014,美国"

我想从中提取“Cupertino, CA”部分。 我编写了以下代码:

NSArray *myArray = [result5 componentsSeparatedByString:@","];
NSLog(@"Response: %@", myArray);
NSString * state = [[myArray objectAtIndex:4]
               stringByReplacingOccurrencesOfRegex:@"[^0-9]" withString:@""];
NSLog(@"Response9: %@", state);
NSString *city = [NSString stringWithFormat:@"%@ %@", 
                           [myArray objectAtIndex:3], state];
NSLog(@"Response1: %@", city);

但我收到了该行的警告:

NSString * state = [[myArray objectAtIndex:4]
             stringByReplacingOccurrencesOfRegex:@"[^0-9]" withString:@""];

其中显示“未找到 -stringByReplacingOccurrenceoOfRegexwithString 方法”和“没有匹配方法签名的消息将被假定返回“id”并接受“.......” ' 作为参数”。

如何从结果中获取州和城市名称?

I making an HTTP request in Objective-C and I get the reply from that is

200,8,"7 Infinite Loop, Cupertino, CA 95014, USA"

I want to extract the part "Cupertino, CA" from it.
I wrote the following code:

NSArray *myArray = [result5 componentsSeparatedByString:@","];
NSLog(@"Response: %@", myArray);
NSString * state = [[myArray objectAtIndex:4]
               stringByReplacingOccurrencesOfRegex:@"[^0-9]" withString:@""];
NSLog(@"Response9: %@", state);
NSString *city = [NSString stringWithFormat:@"%@ %@", 
                           [myArray objectAtIndex:3], state];
NSLog(@"Response1: %@", city);

But I got a warning for the line:

NSString * state = [[myArray objectAtIndex:4]
             stringByReplacingOccurrencesOfRegex:@"[^0-9]" withString:@""];

which says "no -stringByReplacingOccurrenceoOfRegexwithString method found" and "Message without a matching method signature will be assumed to return 'id' and accept '.......' as arguments".

How do I get the state and city name from the result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-08-28 10:15:22

查看[componentsSeparatedByCharactersInSet:][1]。如果您提供数字作为集合,您将获得一个字符串数组,您可以将其重新组合成一个无数字的字符串。

Have a look at [componentsSeparatedByCharactersInSet:][1]. If you supply numbers as the set you will get an array of strings which you can recombine into a numberless string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文