假设我有www.GOOgle.com / .......
我想将其更改为www.google.com / ....
并保持其余的url原样。
我曾尝试使用NSURLComponents
,但它没有用。
// I am taking input from textfield and making the nsurl.
NSURLComponents *components = [NSURLComponents componentsWithString: _textfield.text]; // input from textfield
[[components host] lowercaseString];
NSURL *urlin = [components URL]; //but this gives, www.GOOgle.com
任何领导都表示赞赏。
正如@Larme建议你可以在url中使用setHost方法
见下面的例子
NSURLComponents *components = [NSURLComponents componentsWithString: @"https://sTackoverFlow.com/questions/47924276/how-to-convert-host-of-nsurl-to-lowercase"];
[components setHost:[components.host lowercaseString] ];
NSLog(@"%@",components.URL)
H ttps://stackoverflow.com/questions/47924276/how-to-convert-host-of-nsurl-to-lowercase
注意:
http://需要在String中添加,否则你将获得主机nil,例如https://www.sTackoverFlow.com/questions/47924276/how-to-convert-host-of-nsurl-to-lowercase它将起作用
而
呜呜呜.stack overflow.com/questions/47924276/how-to-convert-host-of-NS URL-to-lowercase
如果你的字符串只是url那么你可以尝试这个,
let strURL = "http://GOogLe.Com/Testt/xyz"
let url = NSURL(string: strURL)
let domain: String = (url?.host)! //get your host name
print(domain) //GOogLe.Com
let str = strURL.replacingOccurrences(of: domain, with: domain.lowercased())
print(str) //http://google.com/Testt/xyz
示例:NSString * lowerCaseStringValue = [_textfield.text lowercaseString]; [NSURLComponents componentsWithString:lowerCaseStringValue];