Reputation: 2410
I have a NSString
and I want to extract a part between two substrings from this string.
Example string: https://itunes.apple.com/de/app/limbo/id656951157?l=en&mt=8
How to get the app id 656951157
, which is between id
and ?
?
Upvotes: 0
Views: 203
Reputation: 14995
Try below using regularexpression:-
NSString *urlString = @"https://itunes.apple.com/de/app/limbo/id656951157?l=en&mt=8";
NSString *URLRegExPattern = @"(?=id).*(?=//?l=en&mt=8 )";
NSError *regExErr;
NSRegularExpression *URLRegEx =
[NSRegularExpression
regularExpressionWithPattern:URLRegExPattern
options:0
error:®ExErr];
NSRange range = [URLRegEx
rangeOfFirstMatchInString:urlString
options:0
range:NSMakeRange(0, urlString.length)];
if (!NSEqualRanges(range,
NSMakeRange(NSNotFound, 0))) {
NSString *appId = [urlString substringWithRange:range];
}
NSLog(@"appId: %@", appId);:-
Upvotes: 0
Reputation: 539685
Instead of searching for substrings ("id", "?") you could convert the string to an URL and get its last path component:
NSString *urlString = @"https://itunes.apple.com/de/app/limbo/id656951157?l=en&mt=8";
NSURL *url = [NSURL URLWithString:urlString];
NSString *lastComp = [url lastPathComponent]; // id656951157
if ([lastComp length] >= 3) {
// Strip initial "id":
NSString *appId = [lastComp substringFromIndex:2];
NSLog(@"%@", appId);
// 656951157
}
Upvotes: 1
Reputation: 1897
Use component separator [yourString componentsSeparatedByString:@"id"];
which will give you an array with 2 values. Second value will be having 656951157?l=en&mt=8
. Again use componentsSeparatedByString
with ? to split this string then you can get 656951157
.
Upvotes: 0