Reputation: 4535
I need to get a string that which is in between ( and ). For example I have a string like "name(123)". I need to get the values of 123 which between ( and ).
Upvotes: 0
Views: 515
Reputation: 14334
NSString *string = @"name(123)";
NSString *seperatorCharsString = @"()";
NSCharacterSet *seperatorCharSet = [NSCharacterSet characterSetWithCharactersInString:seperatorCharsString];
NSArray *components = [string componentsSeparatedByCharactersInSet:seperatorCharSet];
NSString *number = [components lastObject];
Upvotes: 2
Reputation: 11439
You should use a regular expression to extract to the string, you can do that with blocks and NSRegularExpression (since iOS4.0)
Something like this :
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:@"\(.*?\))" options:NSRegularExpressionCaseInsensitive error:nil];
[reg enumerateMatchesInString:<YOUR_STRING> options:0 range:NSMakeRange(0, [<YOUR_STRING> length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
for (int i=0; i < match.numberOfRanges; i++) {
NSRange range = [match rangeAtIndex:i];
NSString *extractedString = [<YOUR_STRING> substringWithRange:range];
}
}];
Hope this helps, Vincent
Upvotes: 2