如何在 iPhone 中拆分字符串?

发布于 2024-11-03 08:40:32 字数 226 浏览 0 评论 0原文

AERP|01,KSE;04,NCEL;05,GSE;|<END> 

这是我的字符串,现在我想将它分成两个数组,例如

array1 = 01,04,05。 array2 = KSE,NCEL,GSE

并忽略 "AERP|" & “|”。

注意:我正在目标 c 中工作。

AERP|01,KSE;04,NCEL;05,GSE;|<END> 

this is my string now i want to split it in two arrays like

array1 = 01,04,05.
array2 = KSE,NCEL,GSE

and ignore "AERP|" & "|<END>".

Note: I am working in objective c.

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

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

发布评论

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

评论(4

鹿港巷口少年归 2024-11-10 08:40:32

假设 |, ;, , 是重要性递减的分隔符,我会首先删除开头和结尾,然后分成数字/助记词成对,然后将它们分开。事情会是这样的:

NSString* input = @"AERP|01,KSE;04,NCEL;05,GSE;|<END>";
NSArray* splits = [input componentsSeparatedByString: @"|"];
// In real life, check that splits has three elements

NSString* body = [splits objectAtIndex: 1];
splits = [body componentsSeparatedByString: @";"];

NSMutableArray* numbers = [[NSMutableArray alloc] init];
NSMutableArray* mnemonics = [[NSMutableArray alloc] init];
for (NSString* item in splits)
{
    NSArray* parts = [item componentsSeparatedByString: @","];
    if ([parts count] == 2)
    {
        [numbers addObject: [parts objectAtIndex: 0]]; // Could convert the part to a number here 
                                                       // with NSDecimalNumber numberWithString: or alternative
        [mnemonics addObject: [parts objectAtIndex: 1]];
    }
}

Assuming that |, ;, , are separators of decreasing significance, I would first remove the beginning and the end, then separate into number/mnemonic pairs, then separate the pairs. It would go something like this:

NSString* input = @"AERP|01,KSE;04,NCEL;05,GSE;|<END>";
NSArray* splits = [input componentsSeparatedByString: @"|"];
// In real life, check that splits has three elements

NSString* body = [splits objectAtIndex: 1];
splits = [body componentsSeparatedByString: @";"];

NSMutableArray* numbers = [[NSMutableArray alloc] init];
NSMutableArray* mnemonics = [[NSMutableArray alloc] init];
for (NSString* item in splits)
{
    NSArray* parts = [item componentsSeparatedByString: @","];
    if ([parts count] == 2)
    {
        [numbers addObject: [parts objectAtIndex: 0]]; // Could convert the part to a number here 
                                                       // with NSDecimalNumber numberWithString: or alternative
        [mnemonics addObject: [parts objectAtIndex: 1]];
    }
}
半世晨晓 2024-11-10 08:40:32

使用 NSString componentsSeparatedByCharactersInSet

- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator

要构造您自己的 NSCharacterSet,请使用 NSMutableCharacterSet

NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString@"|"];

Use the NSString componentsSeparatedByCharactersInSet

- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator

To construct your own NSCharacterSet use NSMutableCharacterSet

NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString@"|"];
抹茶夏天i‖ 2024-11-10 08:40:32
NSString *stringToParse = [string substringWithRange:NSMakeRange(5, [string length] - 7)];
/* stringToParse = @"01,KSE;04,NCEL;05,GSE" */
NSArray *keyValuePairs = [stringToParse componentsSeparatedByString:@";"];
NSMutableArray *indices = [NSMutableArray array];
NSMutableArray *labels = [NSMutableArray array];
for (NSString *keyValueString in keyValuePairs) {
    NSArray *keyValuePairing = [keyValueString componentsSeparatedByString:@","]; 
    [indices addObject:[keyValuePairing objectAtIndex:0]];
    [labels addObject:[keyValuePairing objectAtIndex:1]];
}
NSString *stringToParse = [string substringWithRange:NSMakeRange(5, [string length] - 7)];
/* stringToParse = @"01,KSE;04,NCEL;05,GSE" */
NSArray *keyValuePairs = [stringToParse componentsSeparatedByString:@";"];
NSMutableArray *indices = [NSMutableArray array];
NSMutableArray *labels = [NSMutableArray array];
for (NSString *keyValueString in keyValuePairs) {
    NSArray *keyValuePairing = [keyValueString componentsSeparatedByString:@","]; 
    [indices addObject:[keyValuePairing objectAtIndex:0]];
    [labels addObject:[keyValuePairing objectAtIndex:1]];
}
手长情犹 2024-11-10 08:40:32
NSString *astring=  @"AERP|01,KSE;04,NCEL;05,GSE;|<END>";
    NSArray *aq=[astring componentsSeparatedByString:@"|"];
    NSString *sw=[aq objectAtIndex:1];
    NSArray *aq2=[sw componentsSeparatedByString:@","];
    NSArray *aq3=[[aq2 objectAtIndex:1] componentsSeparatedByString:@";"];
    NSArray *aq4=[[aq2 objectAtIndex:2] componentsSeparatedByString:@";"];
    NSMutableArray *a1,*a2;
    a1=[NSMutableArray array];
    a2=[NSMutableArray array];
    [a1 addObject:[aq2 objectAtIndex:0]];
    [a1 addObject:[aq3 objectAtIndex:1]];
    [a1 addObject:[aq4 objectAtIndex:1]];
    [a2 addObject:[aq3 objectAtIndex:0]];
    [a2 addObject:[aq4 objectAtIndex:0]];
    [a2 addObject:[aq2 objectAtIndex:3]];

修改数组的名称,因为我是匆忙完成的。

NSString *astring=  @"AERP|01,KSE;04,NCEL;05,GSE;|<END>";
    NSArray *aq=[astring componentsSeparatedByString:@"|"];
    NSString *sw=[aq objectAtIndex:1];
    NSArray *aq2=[sw componentsSeparatedByString:@","];
    NSArray *aq3=[[aq2 objectAtIndex:1] componentsSeparatedByString:@";"];
    NSArray *aq4=[[aq2 objectAtIndex:2] componentsSeparatedByString:@";"];
    NSMutableArray *a1,*a2;
    a1=[NSMutableArray array];
    a2=[NSMutableArray array];
    [a1 addObject:[aq2 objectAtIndex:0]];
    [a1 addObject:[aq3 objectAtIndex:1]];
    [a1 addObject:[aq4 objectAtIndex:1]];
    [a2 addObject:[aq3 objectAtIndex:0]];
    [a2 addObject:[aq4 objectAtIndex:0]];
    [a2 addObject:[aq2 objectAtIndex:3]];

modify the names of the array as I have done it in hurry.

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