0

How would I go about removing the "&expires_in=87131" within the following NSString:

Obviously the value 87131 may change in the future and it's always the last part of the string as well.

NSString *accessToken = @"136369349714439%7C2.nNIKZW8Z7Yw_aaaffqKv7lVFYJg__.86400.1301824800-705896566%7Cp-z9A68pJqTDNjEMj0TrHogc2bw&expires_in=87131";
NSString *newStr = [accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
gotnull
  • 26,454
  • 22
  • 137
  • 203

2 Answers2

2

For 'fixing the %7C characters' see NSString stringByReplacingPercentEscapesUsingEncoding

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByReplacingPercentEscapesUsingEncoding

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • 1
    It's not easy actually with the default libraries. A regular expression would be error-prone. Your best bet is to decompose the URL into parameters, remove the one you don't want, then rebuild the URL. You really want a URL parser. There's discussion of one here. http://stackoverflow.com/questions/2225814/nsurl-pull-out-a-single-value-for-a-key-in-a-parameter-string – Jim Blackler Apr 02 '11 at 10:08
2

If the string is guaranteed to only have one ampersand in it:

NSArray *components = [accessToken componentsSeparatedByString:@"&"];
accessToken = [components objectAtIndex:0];
  • I think this will do, I'm pretty certain it will be the first ampersand before that particular string value. – gotnull Apr 02 '11 at 10:47
  • 2
    If not, consider componentsSeparatedByString:@"&expires_in" instead, in case you have a stray & sign in the actual access token (don't believe it can happen but you never know). – Kalle Apr 02 '11 at 11:15
  • Added Kalle's suggestion, thanks for all your help guys. Much much appreciated. – gotnull Apr 03 '11 at 07:08