80

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.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
inFever
  • 1,809
  • 2
  • 16
  • 19

3 Answers3

214

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!"
Emmett
  • 14,035
  • 12
  • 56
  • 81
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
2

Swift 3:

let upperCasedString = "UPPERCASED"

let lowerCasedString = upperCasedString.lowercased() 
//prints "uppercased"
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
1

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)
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223