-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
  • 26,610
  • 9
  • 47
  • 70
puja
  • 209
  • 1
  • 12

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
  • 26,610
  • 9
  • 47
  • 70
Anbu.Karthik
  • 80,161
  • 21
  • 166
  • 138
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,199
  • 3
  • 25
  • 49
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:@""];