Extracting an NSString from both Parenthesis and Quotations

226 Views Asked by At

I have a string that is returned from a WebAPI call that looks like this:

    (
    "[email protected]"
    )

As a workaround, I am trying to extract just the email address i.e. [email protected]

I am not sure what the best approach to do this is as I'm extracting the data within the parenthesis and the quotations.

Any pointers (no pun intended) are appreciated.

2

There are 2 best solutions below

2
On

The easiest way is to use stringByTrimmingCharactersInSet::

NSString *source = /* your source */;
NSCharacterSet *charSet = 
    [NSCharacterSet characterSetWithCharactersInString:@" \"()\n"];
NSString *email = [source stringByTrimmingCharactersInSet:charSet];

Harder one involves NSRegularExpression.

0
On

An even easier one, if you know that the string always starts with (" and ends by "):

NSString *email = [response substringWithRange:NSMakeRange(2, [response length] - 4)];