-4

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.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
puja
  • 209
  • 1
  • 15

3 Answers3

2

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);
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

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);
KAR
  • 3,303
  • 3
  • 27
  • 50
1

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