Hey I am looking for a way to extract a string from another string. It could be any length and be in any part of the string so the usual methods don’t work.
For example
http://bla.com/bla?id=%1234%&something=%888%
What I want to extract is from id=% to the next %.
Any idea’s?
Use the rangeOfString method:
You can then chop up the string using the
substringWithRange:,substringFromIndex:andsubstringToIndex:methods to get the bits you want. Here’s a solution to your specific problem:Alternatively, here’s a general solution for extracting query parameters from a URL, which may be more useful if you need to do this several times:
This doesn’t strip the % characters from the values, but you can do that either with
Or with something like
UPDATE: As of iOS 8+ theres a built-in class called
NSURLComponentsthat can automatically parse query parameters for you (NSURLComponentsis available on iOS 7+, but the query parameter parsing feature isn’t).