从NSString中删除http://

Jac*_*ies 7 iphone objective-c nsstring ipad ios

如何从NSString"http://"中删除某些文字?它需要完全按照这个顺序.谢谢你的帮助!

这是我正在使用的代码,但http://未被删除.取而代之的是http:// http://www.example.com.我该怎么办?谢谢!

NSString *urlAddress = addressBar.text;
[urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
urlAddress = [NSString stringWithFormat:@"http://%@", addressBar.text];
NSLog(@"The user requested this host name: %@", urlAddress);
Run Code Online (Sandbox Code Playgroud)

SVD*_*SVD 19

像这样?

NSString* stringWithoutHttp = [someString stringByReplacingOccurrencesOfString:@"http://" withString:@""];
Run Code Online (Sandbox Code Playgroud)

(如果你只想在开头删除文本,请执行jtbandes所说的内容 - 上面的代码也将替换字符串中间的出现)


小智 8

这是一个解决http和https的解决方案:

    NSString *shortenedURL = url.absoluteURL;

    if ([shortenedURL hasPrefix:@"https://"]) shortenedURL = [shortenedURL substringFromIndex:8];
    if ([shortenedURL hasPrefix:@"http://"]) shortenedURL = [shortenedURL substringFromIndex:7];
Run Code Online (Sandbox Code Playgroud)


jtb*_*des 5

NSString *newString = [myString stringByReplacingOccurrencesOfString:@"http://"
                                                          withString:@""
                                                             options:NSAnchoredSearch // beginning of string
                                                               range:NSMakeRange(0, [myString length])]
Run Code Online (Sandbox Code Playgroud)