如何分割字符串?
我有一个由三个字符串组成的字符串,如下所示
NSString *first = ..;
NSString *second = ..;
NSString *third = ..;
NSString joinedString;
joinedString = [NSString stringWithFormat:@"%@ - %@ (%@)", first, second, third];
现在我需要从 joinedString
取回三个原始字符串。我在某处读到我应该使用 NSScanner 来实现此目的。有什么想法吗?
I have a string made of three strings like this
NSString *first = ..;
NSString *second = ..;
NSString *third = ..;
NSString joinedString;
joinedString = [NSString stringWithFormat:@"%@ - %@ (%@)", first, second, third];
Now I need to get back the three original strings from joinedString
. I've read somewhere that I should use NSScanner
for this. Any ideas how that might work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于您在问题中提供的示例,您可以执行以下操作:
我将使用 NSScanner 和具有可变信息的较大字符串,并且您希望大海捞针。
Given the example you provided in your question, you could do the following:
I would use NSScanner with larger strings that have variable information and you want to find that needle in the haystack.
如果您得到了帖子中提到的确切模式,您可以使用 NSRegularExpression (iOS 4+):(
没有错误处理,现在无法验证它)
If you got the exact pattern as mentioned in your post you can use NSRegularExpression (iOS 4+):
(no error handling and not able to verify it right now)
您可以使用
componentsSeparatedByCharactersInSet:
:然后您可以修剪开始/结束空格:
You can use
componentsSeparatedByCharactersInSet:
:You can then trim the beginning/end spaces: