如何分割字符串?

发布于 2024-10-27 10:26:12 字数 315 浏览 2 评论 0原文

我有一个由三个字符串组成的字符串,如下所示

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 技术交流群。

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

发布评论

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

评论(3

梦与时光遇 2024-11-03 10:26:13

鉴于您在问题中提供的示例,您可以执行以下操作:

// characters we are going to split at
NSString *sep = @" -()";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
NSArray *strings = [joinedString componentsSeparatedByCharactersInSet:set];

// This is the simple way provided that the strings doesn't have spaces in them.

我将使用 NSScanner 和具有可变信息的较大字符串,并且您希望大海捞针。

Given the example you provided in your question, you could do the following:

// characters we are going to split at
NSString *sep = @" -()";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
NSArray *strings = [joinedString componentsSeparatedByCharactersInSet:set];

// This is the simple way provided that the strings doesn't have spaces in them.

I would use NSScanner with larger strings that have variable information and you want to find that needle in the haystack.

魔法少女 2024-11-03 10:26:13

如果您得到了帖子中提到的确切模式,您可以使用 NSRegularExpression (iOS 4+):(

NSString *regexStr = @"(.*)\s-\s(.*)\s\((.*)\)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:nil];
NSArray *results = [regex matchesInString:joinedString options:0 range:NSMakeRange(0, [joinedString length])];
NSString *string1 = [results objectAtIndex:0];
...

没有错误处理,现在无法验证它)

If you got the exact pattern as mentioned in your post you can use NSRegularExpression (iOS 4+):

NSString *regexStr = @"(.*)\s-\s(.*)\s\((.*)\)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:nil];
NSArray *results = [regex matchesInString:joinedString options:0 range:NSMakeRange(0, [joinedString length])];
NSString *string1 = [results objectAtIndex:0];
...

(no error handling and not able to verify it right now)

放飞的风筝 2024-11-03 10:26:13

您可以使用 componentsSeparatedByCharactersInSet:

NSString *chars = @"-()";
NSArray *split = [joinedString componentsSeparatedByCharactersInSet:[NSCharacterset characterSetWithCharactersInString:chars]];

然后您可以修剪开始/结束空格:

stringFromArray = [stringFromArray stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

You can use componentsSeparatedByCharactersInSet::

NSString *chars = @"-()";
NSArray *split = [joinedString componentsSeparatedByCharactersInSet:[NSCharacterset characterSetWithCharactersInString:chars]];

You can then trim the beginning/end spaces:

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