I have a string (IN)+91 but I want to get only +91 and remove characters within ()
, i.e (IN)
. How to do it in iOS with objective-c ?
Any help will be appreciated.
I have a string (IN)+91 but I want to get only +91 and remove characters within ()
, i.e (IN)
. How to do it in iOS with objective-c ?
Any help will be appreciated.
Choice - 1
NSString *final=@"(IN)+91";
NSArray *tempArray = [final componentsSeparatedByString:@")"];
final = [tempArray lastObject];
NSLog(@"%@", final);
Choice - 2
NSRange range1 = [final rangeOfString:@")" options: NSBackwardsSearch];
NSString *newString = [final substringFromIndex:(range1.location+1)];
NSLog(@"%@",newString);
Use this,
NSString *myString = @"(IN)+91";
NSArray *subStrings = [myString componentsSeparatedByString:@")"];
NSString *firstString = [subStrings objectAtIndex:0];
NSString *lastString = [subStrings objectAtIndex:1];
NSLog(@"first - %@, second - %@", firstString, lastString);
Use Regex to replace all matched text at once
NSError *error;
NSString *pattern = @"\([^\)]*\)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSString *afterText = [regex stringByReplacingMatchesInString:beforeText options:0 range:range withTemplate:@""];