如何查看NSString是否以某个其他字符串开头? [英] How to see if an NSString starts with a certain other string?

查看:116
本文介绍了如何查看NSString是否以某个其他字符串开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查我将用作URL的字符串是否以http开头。我现在试图检查的方式似乎不起作用。这是我的代码:

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];

任何想法?

推荐答案

试试这个: if([myString hasPrefix:@http])

顺便说一下,你的测试应该是!= NSNotFound 而不是 == NSNotFound 。但是说你的网址是 ftp://my_http_host.com/thing ,它会匹配,但不应该。

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.

这篇关于如何查看NSString是否以某个其他字符串开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆