0

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

    (
    "username@domain.com"
    )

As a workaround, I am trying to extract just the email address i.e. username@domain.com

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.

Jeremy
  • 161
  • 2
  • 12
  • Doesn't by accident the web client return JSON? Like `["email@domain.com"]`? –  Aug 02 '12 at 12:06

2 Answers2

2

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.

coverback
  • 4,413
  • 1
  • 19
  • 31
  • Thanks. Tried both the NSMakeRange and stringByTrimmingCharactersInSet and both times I get an error: -[__NSArrayI length]: unrecognized selector sent to instance 0x4e42c0 – Jeremy Aug 02 '12 at 12:27
  • That means that your variable isn't a string, it's an `NSArray`. Try printing to log `[source objectAtIndex:0]`, maybe your parentheses are just part of the array notation, not of a string. – coverback Aug 02 '12 at 12:33
0

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)];