ios 在Objective-C中将所有文本转换为小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8693741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:05:15  来源:igfitidea点击:

Converting all text to lower case in Objective-C

objective-cioslowercase

提问by inFever

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.

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

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

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

回答by David R?nnqvist

There is a method called lowercaseStringon NSString. NSStringcontains plenty of methods for string manipulation, please read the documentation.

有一个叫方法lowercaseStringNSStringNSString包含大量的字符串操作方法,请阅读文档

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

回答by Josh O'Connor

Swift 3:

斯威夫特 3:

let upperCasedString = "UPPERCASED"

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

回答by Tamás Sengel

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

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

Objective-C

目标-C

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

Swift 3+

斯威夫特 3+

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