Skip to content

Instantly share code, notes, and snippets.

@tmdvs
Created September 2, 2012 19:34
Show Gist options
  • Save tmdvs/3603700 to your computer and use it in GitHub Desktop.
Save tmdvs/3603700 to your computer and use it in GitHub Desktop.
Help my broken brains
The following doesnt work for some reason and it's really hurting my brains.
The variables contents are:
===========================
myString = <script src="/mint/?js" type="text/javascript">
matchedString = ?js"
I'm finding the range:
======================
NSRange matchRange = [[myString string] rangeOfString:matchedString options:NSLiteralSearch];
The output is as follows:
=========================
matchRange.location = 0.000000;
matchRange.length = 0.000000;
@Gi-lo
Copy link

Gi-lo commented Sep 2, 2012

Can you ensure that the NSString is not nil? rangeOfString: on a nil NSStringreturnslocationandlength` 0.

@tmdvs
Copy link
Author

tmdvs commented Sep 2, 2012

NSLog(@"%@", s.string);
NSLog(@"%@", match);          
NSRange location = [s.string rangeOfString:match options:NSLiteralSearch];
NSLog(@"Loc: %f, Len %f", location.location, location.length);

outputs:

[Line 109] ?js"
[Line 110] <script src="/mint/?js" type="text/javascript"></script>
[Line 112] Loc: 0.000000, Len 0.000000

@a2
Copy link

a2 commented Sep 2, 2012

What happens if you search without the NSLiteralSearch option?

@a2
Copy link

a2 commented Sep 2, 2012

Try switching s.string and match? It seems like you're looking for the big string in the little string?

@tarnfeld
Copy link

tarnfeld commented Sep 2, 2012

Have you tried escaping the quote and/or the question mark with a \? Unlikely that would have any effect, but worth a try...

@tmdvs
Copy link
Author

tmdvs commented Sep 2, 2012

This is a much more realworld example. As you can see there is a ? in the search string!

NSRange location = [s.string rangeOfString:@"?" options:NSAnchoredSearch];
NSLog(@"%@", s.string);
NSLog(@"Loc: %d, Len %d", location.location, location.length);

outputs:

[Line 110]             <script src="/mint/?js" type="text/javascript"></script><script type="text/javascript" src="http://tmdvs.me/mint/?record&amp;key=32664866363337313636345433584f7873686963645471474b31&amp;referer=&amp;resource=http%3A//tmdvs.com/&amp;resource_title=Tim%20Davies%20sans%20vowels&amp;resource_title_encoded=0&amp;resolution=768x1024&amp;flash_version=0&amp;1346617362934&amp;serve_js"></script>

[Line 111] Loc: 2147483647, Len 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment