Converting all text to lower case in Objective-C

倖福魔咒の 提交于 2019-11-27 20:14:51

问题


I have a textfield in my iOS app where the user is supposed to input some text. But I was wondering if there is any way to convert the users input to lowercase letters.

I remember in C# it was something like Convert.ToLower but I can't seem to figure out how to do it in Objective-C.


回答1:


There is a method called lowercaseString on NSString. NSString contains plenty of methods for string manipulation, please read the documentation.

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



回答2:


Swift 3:

let upperCasedString = "UPPERCASED"

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



回答3:


If you want to use a specific Locale when converting a string to lowercase, use lowercaseStringWithLocale (lowercased(with: Locale) in Swift 3+).

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)


来源:https://stackoverflow.com/questions/8693741/converting-all-text-to-lower-case-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!