Remove http: // from NSString

How to remove specific text from an NSString , for example, "http: //"? It must be exactly in that order. Thank you for your help!

Here is the code I'm using, however http: // is not deleted. Instead, it displays 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); 
+7
source share
12 answers

Like this?

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

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

+18
source

Here is a solution that takes care of http and https:

  NSString *shortenedURL = url.absoluteURL; if ([shortenedURL hasPrefix:@"https://"]) shortenedURL = [shortenedURL substringFromIndex:8]; if ([shortenedURL hasPrefix:@"http://"]) shortenedURL = [shortenedURL substringFromIndex:7]; 
+7
source
 NSString *newString = [myString stringByReplacingOccurrencesOfString:@"http://" withString:@"" options:NSAnchoredSearch // beginning of string range:NSMakeRange(0, [myString length])] 
+4
source

Another way:

 NSString *str = @"http//abc.com"; NSArray *arr = [str componentSeparatedByString:@"//"]; NSString *str1 = [arr objectAtIndex:0]; // http NSString *str2 = [arr objectAtIndex:1]; // abc.com 
+3
source

if http: // is at the beginning of the line you can use

  NSString *newString = [yourOriginalString subStringFromIndex:7]; 

or as SVD suggested

EDIT: Take the EDIT question

change this line

 [urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""]; 

to

 urlAddress = [urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""]; 
+3
source

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]; } 
0
source

Here is another option:

 NSMutableString *copiedUrl = [[urlAddress mutablecopy] autorelease]; [copiedUrl deleteCharactersInRange: [copiedUrl rangeOfString:@"http://"]]; 
0
source

NSString * newString = [string stringByReplacingOccurrencesOfString: @ "http: //" withString: @ ""];

0
source

If you want to crop 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 the specified prefixes from the front and suffixes from the back (as a trailing slash). Just add extra substrings to the prefix / suffix array to filter more.

0
source

Swift 3

To replace all occurrences:

 let newString = string.replacingOccurrences(of: "http://", with: "") 

To replace occurrences at the beginning of a line:

 let newString = string.replacingOccurrences(of: "http://", with: "", options: .anchored) 
0
source

Hi guys, a little late, but I came with a common way. Let them 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" 
0
source

Another common 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; } 
0
source

All Articles