2013-05-09 56 views
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); 

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

+0

更换\"我认为你有这三个字符串1.“Level1,Row 1,Gold,Seat no 7”2.“Level1,Row 1,Gold,Seat no 8”3.“Name:K阿兰”。我对吗 ? – 2013-05-09 06:33:38

+0

@DharmbirChoudhary只是从扫描票证中得到的一个字符串。这是我得到的完整字符串。 – 2013-05-09 06:39:07

+0

我想你有你的答案,所以需要expalin它.. – 2013-05-09 06:40:04

回答

1

尝试这样it'l给出正确的输出,

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 

在你的代码"

+0

在哪里取代。 – 2013-05-09 06:23:08

+0

@iPhone:根据您的要求,您可以使用我的代码中的stringURL字符串替换您的字符串。 – Balu 2013-05-09 06:32:02

0

使用这种NSString的方法。

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