我有一个字符串(IN)+91,但我想只获得+91并删除()
中的字符,即(IN)
。如何在iOS中使用objective-c?
任何帮助将不胜感激。
答案 0 :(得分:2)
选择 - 1
NSString *final=@"(IN)+91";
NSArray *tempArray = [final componentsSeparatedByString:@")"];
final = [tempArray lastObject];
NSLog(@"%@", final);
选择 - 2
NSRange range1 = [final rangeOfString:@")" options: NSBackwardsSearch];
NSString *newString = [final substringFromIndex:(range1.location+1)];
NSLog(@"%@",newString);
答案 1 :(得分:1)
使用此,
NSString *myString = @"(IN)+91";
NSArray *subStrings = [myString componentsSeparatedByString:@")"];
NSString *firstString = [subStrings objectAtIndex:0];
NSString *lastString = [subStrings objectAtIndex:1];
NSLog(@"first - %@, second - %@", firstString, lastString);
答案 2 :(得分:1)
使用Regex一次替换所有匹配的文本
NSError *error;
NSString *pattern = @"\([^\)]*\)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSString *afterText = [regex stringByReplacingMatchesInString:beforeText options:0 range:range withTemplate:@""];