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

问题描述 投票:0回答:3

假设我有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

任何领导都表示赞赏。

ios objective-c nsurl nsurlcomponents
3个回答
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)

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

不管用


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

-1
投票
  1. 将字符串转换为小写。
  2. 然后将转换后的字符串值传递给componentsWithString方法。

示例:NSString * lowerCaseStringValue = [_textfield.text lowercaseString]; [NSURLComponents componentsWithString:lowerCaseStringValue];

© www.soinside.com 2019 - 2024. All rights reserved.