nsmutablestring - Objective C - Extract substring by finding first and second occurrence of a character -
i trying port android app ios. need extract string present between first , second occurrence of single quote '
character.
for example, javascript:popupwindow7('news_details.asp?slno=2029',620,300,100,100,'yes')
, need extract news_details.asp?slno=2029
.
in java, 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 method similar indexof in objective c, did following:
nsuinteger length =0; nsmutablestring* url; nsstring* urltodecode = @"javascript:popupwindow7('news_details.asp?slno=2029',620,300,100,100,'yes')"; (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 doing wrong?
the problem code range's length
counted location zero, not range's location
. range's length
not index @ end of range, distance between range's starting location , end.
how simpler alternative:
nsarray *components = [urltodecode componentsseparatedbystring:@"'"]; if (components.count > 1) { nsstring *substring = components[1]; }
Comments
Post a Comment