删除具有特殊字符Objective-c iOS的字符串

时间:2016-11-22 10:33:15

标签: ios objective-c

我有一个字符串(IN)+91,但我想只获得+91并删除()中的字符,即(IN)。如何在iOS中使用objective-c?

任何帮助将不胜感激。

3 个答案:

答案 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:@""];