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];
2
  • Is it always the last part of the string? Commented Apr 2, 2011 at 10:29
  • Correct, it's always the last part of the string. Commented Apr 2, 2011 at 10:31

2 Answers 2

2

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

NSArray *components = [accessToken componentsSeparatedByString:@"&"];
accessToken = [components objectAtIndex:0];
Sign up to request clarification or add additional context in comments.

3 Comments

I think this will do, I'm pretty certain it will be the first ampersand before that particular string value.
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).
Added Kalle's suggestion, thanks for all your help guys. Much much appreciated.
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

1 Comment

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. stackoverflow.com/questions/2225814/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.