Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting all text to lower case in Objective-C

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.

like image 264
inFever Avatar asked Jan 01 '12 15:01

inFever


3 Answers

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!"
like image 161
David Rönnqvist Avatar answered Nov 18 '22 06:11

David Rönnqvist


Swift 3:

let upperCasedString = "UPPERCASED"

let lowerCasedString = upperCasedString.lowercased() 
//prints "uppercased"
like image 24
Josh O'Connor Avatar answered Nov 18 '22 07:11

Josh O'Connor


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)
like image 30
Tamás Sengel Avatar answered Nov 18 '22 07:11

Tamás Sengel