如何编辑此字符串

时间:2013-05-09 06:08:45

标签: iphone string

这是一个字符串

 "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);

如何实现这一点请指导。

2 个答案:

答案 0 :(得分: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);

O / P: -

Level1, Row 1, Gold, Seat no 7, Level1, Row 1, Gold, Seat no 8, Name: Karan
代码中的

\"替换为"

答案 1 :(得分:0)

使用NSString的这种方法。

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement