How to see if an NSString starts with a certain other string? How to see if an NSString starts with a certain other string? ios ios

How to see if an NSString starts with a certain other string?


Try this: if ([myString hasPrefix:@"http"]).

By the way, your test should be != NSNotFound instead of == NSNotFound. But say your URL is ftp://my_http_host.com/thing, it'll match but shouldn't.


I like to use this method:

if ([[temp substringToIndex:4] isEqualToString:@"http"]) {  //starts with http}

or even easier:

if ([temp hasPrefix:@"http"]) {    //do your stuff}


If you're checking for "http:" you'll probably want case-insensitive search:

NSRange prefixRange =     [temp rangeOfString:@"http"                 options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];if (prefixRange.location == NSNotFound)