Reputation: 13267
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);
Upvotes: 7
Views: 8953
Reputation: 367
This String extension adds a removeHTTPPrefix()
method that removes the http://
or https://
prefix from a URL by splitting the string at ://
and returning the part after it, or the original string if no separator is found:
extension String {
func removeHTTPPrefix() -> String {
return self.components(separatedBy: "://").last ?? self
}
}
let url = "https://example.com"
let result = url.removeHTTPPrefix()
// result: "example.com"
let plainString = "example.com"
let result2 = plainString.removeHTTPPrefix()
// result2: "example.com" (no change, since there's no "http://" or "https://")
Upvotes: 1
Reputation: 1987
This will remove any scheme including http, https, etc.
NSRange dividerRange = [str rangeOfString:@"://"];
NSString *newString = [str substringFromIndex:NSMaxRange(dividerRange)];
Upvotes: 0
Reputation: 515
Since the thread is still active and showed up in my search to remove the prefix from a URL (not NSString)... there is a one-liner if you are starting with a URL:
String(url.absoluteString.dropFirst((url.scheme?.count ?? -3) + 3))
Upvotes: 2
Reputation: 1253
For those using swift and have arrived here.
extension String {
func withoutHttpPrefix() -> String {
var idx: String.Index?;
if self.hasPrefix("http://www.") {
idx = self.index(startIndex, offsetBy: 11)
} else if hasPrefix("https://www.") {
idx = self.index(startIndex, offsetBy: 12)
} else if self.hasPrefix("http://") {
idx = self.index(startIndex, offsetBy: 7)
} else if hasPrefix("https://") {
idx = self.index(startIndex, offsetBy: 8)
}
if idx != nil {
return String(self[idx!...])
}
return self
}
}
Upvotes: 1
Reputation: 144
Hi guys bit late but I come with a generic way Let's say:
NSString *host = @"ssh://www.somewhere.com";
NSString *scheme = [[[NSURL URLWithString:host] scheme] stringByAppendingString:@"://"];
// This extract ssh and add :// so we get @"ssh://" note that this code handle any scheme http, https, ssh, ftp ....
NSString *stripHost = [host stringByReplacingOccurrencesOfString:scheme withString:@""];
// Result : stripHost = @"www.somewhere.com"
Upvotes: 0
Reputation: 4818
One more general way:
- (NSString*)removeURLSchemeFromStringURL:(NSString*)stringUrl {
NSParameterAssert(stringUrl);
static NSString* schemeDevider = @"://";
NSScanner* scanner = [NSScanner scannerWithString:stringUrl];
[scanner scanUpToString:schemeDevider intoString:nil];
if (scanner.scanLocation <= stringUrl.length - schemeDevider.length) {
NSInteger beginLocation = scanner.scanLocation + schemeDevider.length;
stringUrl = [stringUrl substringWithRange:NSMakeRange(beginLocation, stringUrl.length - beginLocation)];
}
return stringUrl;
}
Upvotes: 0
Reputation: 474
Swift 3
For replacing all occurrences:
let newString = string.replacingOccurrences(of: "http://", with: "")
For replacing occurrences at the start of the string:
let newString = string.replacingOccurrences(of: "http://", with: "", options: .anchored)
Upvotes: 1
Reputation: 2431
In case you wish to trim both sides and also write less code:
NSString *webAddress = @"http://www.google.co.nz";
// add prefixes you'd like to filter out here
NSArray *prefixes = [NSArray arrayWithObjects:@"https:", @"http:", @"//", @"/", nil];
for (NSString *prefix in prefixes)
if([webAddress hasPrefix:prefix]) webAddress = [webAddress stringByReplacingOccurrencesOfString:prefix withString:@"" options:NSAnchoredSearch range:NSMakeRange(0, [webAddress length])];
// add suffixes you'd like to filter out here
NSArray *suffixes = [NSArray arrayWithObjects:@"/", nil];
for (NSString *suffix in suffixes)
if([webAddress hasSuffix:suffix]) webAddress = [webAddress stringByReplacingOccurrencesOfString:suffix withString:@"" options:NSBackwardsSearch range:NSMakeRange(0, [webAddress length])];
This code will remove specified prefixes from the front and suffixes from the back (like a trailing slash). Simply add more substrings to the prefix/suffix array to filter for more.
Upvotes: 1
Reputation: 339
Here's a solution which takes care of http & https:
NSString *shortenedURL = url.absoluteURL;
if ([shortenedURL hasPrefix:@"https://"]) shortenedURL = [shortenedURL substringFromIndex:8];
if ([shortenedURL hasPrefix:@"http://"]) shortenedURL = [shortenedURL substringFromIndex:7];
Upvotes: 9
Reputation: 798
NSString* newString = [string stringByReplacingOccurrencesOfString:@"http://" withString:@""];
Upvotes: 0
Reputation: 21154
Here is another option;
NSMutableString *copiedUrl = [[urlAddress mutablecopy] autorelease];
[copiedUrl deleteCharactersInRange: [copiedUrl rangeOfString:@"http://"]];
Upvotes: 1
Reputation: 3319
OR
+(NSString*)removeOpeningTag:(NSString*)inString tag:(NSString*)inTag {
if ([inString length] == 0 || [inTag length] == 0) return inString;
if ([inString length] < [inTag length]) {return inString;}
NSRange tagRange= [inString rangeOfString:inTag];
if (tagRange.location == NSNotFound || tagRange.location != 0) return inString;
return [inString substringFromIndex:tagRange.length];
}
Upvotes: 0
Reputation: 34286
if http:// is at the start of the string you can use
NSString *newString = [yourOriginalString subStringFromIndex:7];
or else as SVD suggested
EDIT: AFter seeing question EDIT
change this line
[urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
to
urlAddress = [urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
Upvotes: 3
Reputation: 14123
Another way is :
NSString *str = @"http//abc.com";
NSArray *arr = [str componentSeparatedByString:@"//"];
NSString *str1 = [arr objectAtIndex:0]; // http
NSString *str2 = [arr objectAtIndex:1]; // abc.com
Upvotes: 2
Reputation: 118751
NSString *newString = [myString stringByReplacingOccurrencesOfString:@"http://"
withString:@""
options:NSAnchoredSearch // beginning of string
range:NSMakeRange(0, [myString length])]
Upvotes: 6
Reputation: 4753
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)
Upvotes: 19