How to see if an NSString starts with a certain other string?
70,412
Solution 1
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.
Solution 2
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
}
Solution 3
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)
Solution 4
Swift version:
if line.hasPrefix("#") {
// checks to see if a string (line) begins with the character "#"
}
Related videos on Youtube
Author by
Rob
Updated on May 16, 2020Comments
-
Rob almost 4 years
I am trying to check to see if a string that I am going to use as URL starts with http. The way I am trying to check right now doesn't seem to be working. Here is my code:
NSMutableString *temp = [[NSMutableString alloc] initWithString:@"http://"]; if ([businessWebsite rangeOfString:@"http"].location == NSNotFound){ NSString *temp2 = [[NSString alloc] init]; temp2 = businessWebsite; [temp appendString:temp2]; businessWebsite = temp2; NSLog(@"Updated BusinessWebsite is: %@", businessWebsite); } [web setBusinessWebsiteUrl:businessWebsite];
Any ideas?
-
Rob over 12 yearsYup that was it. I should have noticed the != thing before, but in the end it was the hasPrefix that worked. Thanks for the advice, I'll mark yours as the correct answer as soon as it lets me.
-
Rob over 12 yearsThats good too. This way is a little more flexible as well, thanks for the comment
-
A . Radej over 11 yearsThis will crash if temp string is less than 5 characters. Index starts at 0. So this is not a good answer. Also, the example has a character count mismatch: "http" does not have 5 characters. Case insensitivity should also be considered.
-
JonasG over 11 years@Daniel What are you even saying? Why 5? This is not an NSArray... Index 4 is the 4th character not the 5th! And have you EVER seen Http or hTtP? Case sensitive is not relevant. Also the question was about checking if the string begins with http not about the string being shorter than 4 characters. hasPrefix: is better but this works just as well. Stop whining
-
A . Radej over 11 years@JonasG - Yes, you are correct about the behavior of substringToIndex. Note, however, index 4 is actually the 5th character; index 0 is the first character. I had mistakenly assumed that substringToIndex includes the character specified by the index, but it does not. Case sensitivity is relevant when user input is involved, which I believe the question hints at. Consider the case of "HTTP://WWW...". But the biggest problem is that the proposed solution will throw an exception when it encounters "ftp" or a string less than 4 characters. The hasPrefix method does not have the same problem.
-
Richard over 8 yearsI don't know why this was down-voted... this is the simple Swift way of doing this. Most new iOS developers are probably going to be using Swift from here on out, and the OP never said that only Objective-C answers were requested.
-
superarts.org almost 8 years"I don't know why this was down-voted"- probably because the syntax is wrong? Should be
if line.hasPrefix("prefix")
{}` -
Richard almost 8 yearsThanks for pointing out a simpler syntax, but placing () around an if statement isn't bad syntax. For some of us old timers, it reads more clearly, and works exactly the same.
if (line.hasPrefix("#")) {}
works just as well.