如何将nsurl的主机转换为小写

时间:2017-12-21 11:44:08

标签: ios objective-c nsurl nsurlcomponents

我们说我有www.GOOgle.com /.......

我想将其更改为www.google.com / ....

并保留url的其余部分。

我尝试了// 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 ,但它没有用。

Nancy.1.4.4
Nancy.Authentication.Forms.1.4.1
Nancy.Hosting.Self.1.4.1
Nancy.Viewengines.Razor.1.4.3
Microsoft.AspNet.Razor.2.0.30506.0

任何领导都表示赞赏。

3 个答案:

答案 0 :(得分:2)

作为@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)
  

ħ   TTPS://stackoverflow.com/questions/47924276/how-to-convert-host-of-nsurl-to-lowercase

注意:

添加String需要

http:// ,否则您将获得主机nil 例如https://www.sTackoverFlow.com/questions/47924276/how-to-convert-host-of-nsurl-to-lowercase它会起作用

,而

www.sTackoverFlow.com/questions/47924276/how-to-convert-host-of-nsurl-to-lowercase

不起作用

答案 1 :(得分:0)

如果你的字符串只是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

答案 2 :(得分:-1)

  1. 将字符串转换为小写。
  2. 然后将转换后的字符串值传递给componentsWithString方法。
  3. <强>示例:  NSString * lowerCaseStringValue = [_textfield.text lowercaseString]; [NSURLComponents componentsWithString:lowerCaseStringValue];