【问题标题】:Converting all text to lower case in Objective-C在 Objective-C 中将所有文本转换为小写
【发布时间】:2012-01-31 09:34:47
【问题描述】:

我的 iOS 应用程序中有一个文本字段,用户应该在其中输入一些文本。但我想知道是否有任何方法可以将用户输入转换为小写字母。

我记得在 C# 中它类似于 Convert.ToLower 但我似乎无法弄清楚如何在 Objective-C 中做到这一点。

【问题讨论】:

    标签: objective-c ios lowercase


    【解决方案1】:

    如果您想在将字符串转换为小写时使用特定的Locale,请使用lowercaseStringWithLocale(在Swift 3+ 中为lowercased(with: Locale))。

    Objective-C

    NSString *myString = @"Hello, World!";
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    NSString *lower = [myString lowercaseStringWithLocale:locale];
    

    Swift 3+

    let myString = "Hello, World!"
    let locale = Locale(identifier: "en_US")
    let lower = myString.lowercased(with: locale)
    

    【讨论】:

      【解决方案2】:

      斯威夫特 3:

      let upperCasedString = "UPPERCASED"
      
      let lowerCasedString = upperCasedString.lowercased() 
      //prints "uppercased"
      

      【讨论】:

        【解决方案3】:

        NSString 上有一个名为 lowercaseString 的方法。 NSString 包含大量的字符串操作方法,请阅读the documentation

        NSString *myString = @"Hello, World!";
        NSString *lower = [myString lowercaseString]; // this will be "hello, world!"
        

        【讨论】:

        • 或者myString.lowercaseString,以防你需要去某个地方
        猜你喜欢
        • 1970-01-01
        • 2010-11-09
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        • 2011-07-29
        • 2012-08-02
        • 2014-02-19
        • 1970-01-01
        相关资源
        最近更新 更多