Converting all text to lower case in Objective-C

Objective CIosLowercase

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

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!"

Solution 2 - Objective C

Swift 3:

let upperCasedString = "UPPERCASED"

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

Solution 3 - Objective C

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioninFeverView Question on Stackoverflow
Solution 1 - Objective CDavid RönnqvistView Answer on Stackoverflow
Solution 2 - Objective CJosh O'ConnorView Answer on Stackoverflow
Solution 3 - Objective CTamás SengelView Answer on Stackoverflow