如何从
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);
像这样吗?
NSString* stringWithoutHttp = [someString stringByReplacingOccurrencesOfString:@"http://" withString:@""];
(如果您只想删除开头的文本,请执行 jtbandes 所说的操作 - 上面的代码也将替换字符串中间出现的文本)
这是一个处理 http 和 https 的解决方案:
NSString *shortenedURL = url.absoluteURL;
if ([shortenedURL hasPrefix:@"https://"]) shortenedURL = [shortenedURL substringFromIndex:8];
if ([shortenedURL hasPrefix:@"http://"]) shortenedURL = [shortenedURL substringFromIndex:7];
NSString *newString = [myString stringByReplacingOccurrencesOfString:@"http://"
withString:@""
options:NSAnchoredSearch // beginning of string
range:NSMakeRange(0, [myString length])]
如果 http:// 位于字符串的开头,您可以使用
NSString *newString = [yourOriginalString subStringFromIndex:7];
或者按照 SVD 的建议
编辑:看到问题后编辑
更改此行
[urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
到
urlAddress = [urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
另一种方法是:
NSString *str = @"http//abc.com";
NSArray *arr = [str componentSeparatedByString:@"//"];
NSString *str1 = [arr objectAtIndex:0]; // http
NSString *str2 = [arr objectAtIndex:1]; // abc.com
由于该线程仍然处于活动状态并出现在我的搜索中,以从 URL(不是 NSString)中删除前缀...如果您从 URL 开始,则会有一行:
String(url.absoluteString.dropFirst((url.scheme?.count ?? -3) + 3))
这是另一种选择;
NSMutableString *copiedUrl = [[urlAddress mutablecopy] autorelease];
[copiedUrl deleteCharactersInRange: [copiedUrl rangeOfString:@"http://"]];
如果您希望修剪两侧并减少编写代码:
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])];
此代码将从前面删除指定的前缀,并从后面删除指定的后缀(如尾部斜杠)。只需向前缀/后缀数组添加更多子字符串即可过滤更多内容。
斯威夫特3
要替换所有出现的情况:
let newString = string.replacingOccurrences(of: "http://", with: "")
用于替换字符串开头的匹配项:
let newString = string.replacingOccurrences(of: "http://", with: "", options: .anchored)
对于那些使用 swift 并已经到达这里的人。
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
}
}
或
+(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];
}
NSString* newString = [string stringByReplacingOccurrencesOfString:@"http://" withString:@""];
一种更通用的方法:
- (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;
}
大家好,有点晚了,但我有一个通用的方法 让我们说:
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"
这将删除任何方案,包括 http、https 等。
NSRange dividerRange = [str rangeOfString:@"://"];
NSString *newString = [str substringFromIndex:NSMaxRange(dividerRange)];
extension String {
func removeHTTPPrefix() -> String {
return self.components(separatedBy: "://").last ?? self
}
}