代码之家  ›  专栏  ›  技术社区  ›  backbencher

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

  •  0
  • backbencher  · 技术社区  · 7 年前

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

    任何潜在客户都将不胜感激。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Prashant Tukadiya    7 年前

    正如@Larme所建议的,您可以使用方法在url中设置主机

    参见下面的示例

    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:// 必须添加到字符串中,否则将得到主机nil 如 https://www.sTackoverFlow.com/questions/47924276/how-to-convert-host-of-nsurl-to-lowercase 它会起作用的

    虽然

    www.sTackoverFlow。com/questions/47924276/如何将nsurl的主机转换为小写

    不起作用

        2
  •  0
  •   rmaddy    7 年前

    如果你的字符串是唯一的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
    
        3
  •  -1
  •   rmaddy    7 年前
    1. 将字符串转换为小写。
    2. 然后将转换后的字符串值传递给componentsWithString方法。

    样品 : NSString*小写字符串值=[\u textfield.text小写字符串]; [NSURLComponents componentsWithString:小写字符串值];