I am trying to port my Android app to iOS. I need to extract the string present between the first and second occurrence of the single quote '
character.
For example, from javascript:popUpWindow7('news_details.asp?slno=2029',620,300,100,100,'yes')
, I need to extract news_details.asp?slno=2029
.
In Java, I did this:
String inputUrl = "javascript:popUpWindow7('news_details.asp?slno=2029',620,300,100,100,'yes')";
StringBuilder url = new StringBuilder();
url.append(inputUrl.substring(inputUrl.indexOf('\'')+1,
inputUrl.indexOf('\'',inputUrl.indexOf('\'')+1)));
I can't find any method similar to indexOf in Objective C, so I did the following:
NSUInteger length =0;
NSMutableString* url;
NSString* urlToDecode = @"javascript:popUpWindow7('news_details.asp?slno=2029',620,300,100,100,'yes')";
for (NSInteger i=[urlToDecode rangeOfString:@"\'"].location +1; i<urlToDecode.length; i++) {
if([urlToDecode characterAtIndex:i]== '\'')
{
length = i;
break;
}
}
NSRange range = NSMakeRange([urlToDecode rangeOfString:@"\'"].location +1, length);
[url appendString:[urlToDecode substringWithRange:range]];
What am I doing wrong?
The problem with your code is that your range's
length
is counted from location zero, not from the range'slocation
. A range'slength
is not the index at the end of the range, but the distance between the range's starting location and its end.How about this as a simpler alternative: