如何根据字符标记拆分NSString?

时间:2014-08-21 10:47:57

标签: objective-c nsstring

以下是我从网络服务获取的NSString:

"You invited @testaccount,Peter,richie@ to WonUp $#book$"

我需要将字符串testaccount,Peter,richie放入NSArray,将#book(即$标记内的字符串部分)放入另一个NSArray中。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

你可以这样做:

NSString *str = @"@testaccount,Peter,richie@ to WonUp $#book$";
NSArray* arr = [str componentsSeparatedByString:@" to WonUp "];
NSString *str1;
NSArray* arr1;
if (arr.count > 0) {
    str1 = [[arr objectAtIndex:0]stringByReplacingOccurrencesOfString:@"@" withString:@""];
    arr1 = [str1 componentsSeparatedByString:@","];
}
NSString *str2;
if (arr.count > 1) {
    str2 = [[arr objectAtIndex:1]stringByReplacingOccurrencesOfString:@"$" withString:@""];
}
NSLog(@"%@ %@",str2,arr1);