简体   繁体   English

如何轻松分离存储在较大NSString中的一组字符串?

[英]How do I easily separate a set of strings stored in a larger NSString?

I had a UILabel with the following text: 我有一个带有以下文本的UILabel:

Medium, Black

What I intended to do was grab the words in the string and insert each into a mutable array so I could use each title later on to identify something. 我打算做的就是抓住字符串中的单词,然后将每个单词插入可变数组中,以便稍后可以使用每个标题来识别内容。

With the help of Stackoverflow I done it like this: 在Stackoverflow的帮助下,我做到了:

NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByCharactersInSet:
      [NSCharacterSet characterSetWithCharactersInString:@" ,"]] mutableCopy];
[chosenOptions removeObject:@""];

Now I can access this objects and they return the correct strings: 现在,我可以访问此对象,并且它们返回正确的字符串:

NSString *size = [chosenOptions objectAtIndex:0]; //Medium
NSString *colour = [chosenOptions objectAtIndex:1]; //Black

This is fine. 这可以。 But the problem starts when dealing with female sizes instead of males which are displayed like this: 但是问题开始于处理女性尺码而不是像这样显示的男性尺码:

[8 UK], [10 UK], [12 UK], [14 UK] 

Let us say I now have a UILabel with the following text: 假设我现在有一个带有以下文本的UILabel:

[8 UK], Black

Using the same code above my NSLog here: 在我的NSLog上方使用相同的代码:

NSLog(@"size label-> %@", size); 
NSLog(@"colour label-> %@", colour);

Reads back: 回读:

size label-> [8

colour label-> UK]

Would appreciate a simple solution in code please. 请欣赏一个简单的代码解决方案。

The code that does the stripping doesn't take into account the way my female sizes are set in a string. 进行剥离的代码未考虑将女性大小设置为字符串的方式。 I need a solution that will work with both male and female size string styles. 我需要一个适用于男性和女性大小的字符串样式的解决方案。

Thanks for your time. 谢谢你的时间。

只需使用componentsSeparatedByString:@", "

NSArray *chosenOptions = [[[cell tapToEditLabel] text] componentsSeparatedByString:@", "];

Another way to do this that works with trailing commas with or without whitespace. 做到这一点的另一种方法是使用带或不带空格的尾部逗号。

NSString *tag = @"Medium, Black";
NSMutableArray *options = [[tag componentsSeparatedByString:@","] mutableCopy];
NSString *size = [[options objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *color = [[options objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"%@:%@", size, color);

We first split by just , and then remove all extra whitespace. 我们首先按分割,然后删除所有多余的空格。

Here is some sample data: 以下是一些示例数据:

NSArray *tags = @[@"[8 UK], Black", @"Medium, Black", @"Large, Red, ", @"Small, Black,"];
for (NSString *tag in tags) {
    NSMutableArray *options = [[tag componentsSeparatedByString:@","] mutableCopy];
    NSString *size = [[options objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *color = [[options objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSLog(@"%@:%@", size, color);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM