1

这是一个字符串

 "Level1, Row 1, Gold, Seat no 7". "Level1, Row 1, Gold, Seat no 8". Name: Karan.

我正在尝试使这个字符串像这样

Level1, Row 1, Gold, Seat no 7, Level1, Row 1, Gold, Seat no 8, Name: Karan

我必须在此搜索座位号和名称,在此之前我需要将其分隔为数组。这是我正在尝试的方式。

[outStr stringByReplacingOccurrencesOfString:@"." withString:@","];
//[outStr stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSLog(@"outstr:%@", outStr);
NSArray *arrSplit1 = [outStr componentsSeparatedByString:@","];
NSString *stringToSearch = @"seat no";
NSString *stringToSearch1 = @"name";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c]    %@",stringToSearch]; 
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",stringToSearch1];
NSArray *results = [arrSplit1 filteredArrayUsingPredicate:predicate];
NSArray *results1 = [arrSplit1 filteredArrayUsingPredicate:predicate1];
NSLog(@"results:%@", results);
NSLog(@"results:%@", results1);

我怎样才能做到这一点,请指导。

4

2 回答 2

1

像这样尝试它会给出正确的输出,

NSString *stringURL = @"Level1, Row 1, Gold, Seat no 7\". \"Level1, Row 1, Gold, Seat no 8\". Name: Karan.";
    stringURL=[stringURL stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    stringURL=[stringURL stringByReplacingOccurrencesOfString:@"." withString:@","];
    if([stringURL hasSuffix:@","])
        stringURL=[stringURL substringToIndex:[stringURL length]-1];
    NSLog(@"%@",stringURL);

运单:-

Level1, Row 1, Gold, Seat no 7, Level1, Row 1, Gold, Seat no 8, Name: Karan

在您的代码中替换\""

于 2013-05-09T06:17:22.240 回答
0

使用 NSString 的这个方法。

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
于 2013-05-09T06:16:24.323 回答