iphoneobjective-ciosipadnsstring

Remove http:// from NSString


How do I remove certain text from a NSString such as "http://"? It needs to be exactly in that order. Thanks for your help!

Here is the code I am using, however the http:// is not removed. Instead it appears http://http://www.example.com. What should I do? Thanks!

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

Solution

  • Like this?

    NSString* stringWithoutHttp = [someString stringByReplacingOccurrencesOfString:@"http://" withString:@""];
    

    (if you want to remove text at the beginning only, do what jtbandes says - the code above will replace occurrences in the middle of the string as well)