61

Since Strings in Swift no longer have the .uppercaseString or .lowercaseString properties available, how would i go about performing that function?

If I have for example:

var sillyString = "This is a string!"
let yellyString = sillyString.uppercaseString
let silentString = sillyString.lowercaseString

I would like to take sillyString and mutate it into either uppercase or lowercase. How would i go about doing that now?

starball
  • 20,030
  • 7
  • 43
  • 238
Don Alejandro
  • 885
  • 1
  • 7
  • 16

5 Answers5

114

Xcode 6.0 / Swift 1.0

String is bridged seamlessly to NSString, so it does have uppercaseString and lowercaseString properties as long as you import Foundation (or really almost any framework since they'll usually import Foundation internally. From the Strings and Characters section of the Swift Programming Guide:

Swift’s String type is bridged seamlessly to Foundation’s NSString class. If you are working with the Foundation framework in Cocoa or Cocoa Touch, the entire NSString API is available to call on any String value you create, in addition to the String features described in this chapter. You can also use a String value with any API that requires an NSString instance.


Xcode 6.1 / Swift 1.1

As @newacct pointed out, in Xcode 6.1 / Swift 1.1, uppercaseString and lowercaseString are in Swift's String class so you don't need to use the ones defined in NSString. However, it's implemented as an extension to the String class in the Foundation framework so the solution is still the same: import Foundation

In a playground:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercaseString // --> THIS IS A STRING!
let silentString = sillyString.lowercaseString // --> this is a string!

Swift 3.0

In a playground:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercased() // --> THIS IS A STRING!
let silentString = sillyString.lowercased() // --> this is a string!
Alex Haas
  • 2,127
  • 2
  • 17
  • 13
Mike S
  • 41,895
  • 11
  • 89
  • 84
  • `uppercaseString` and `lowercaseString` are defined as properties on `String` in Foundation. `NSString` is not involved. – newacct Oct 08 '14 at 07:28
  • @newacct So they are, thanks for pointing that out. It looks like the change happened between Xcode 6.0 / Swift 1.0 and Xcode 6.1 / Swift 1.1. – Mike S Oct 08 '14 at 15:29
82

Swift 4 & 5

TL;DR

The new names in Swift 3 use the -ed postfix to indicate that uppercased() and lowercased() return a copy rather than a modified original:

import Foundation

let title = "Castration: The Advantages and the Disadvantages" // Don't `var` unless you have to!
title.uppercased() // "CASTRATION: THE ADVANTAGES AND THE DISADVANTAGES"
title.lowercased() // "castration: the advantages and the disadvantages"
title.capitalized  // "Castration: The Advantages And The Disadvantages"

Inconsistencies: .method() vs .property

Note the odd discrepancy where uppercased() and lowercased() are functions, while capitalized is a property. This seems like an oversight, in which case hopefully someone comfortable with the swift evolution process will make a correction before 3.0 leaves beta.

If happen to know you're working with NSString, there is a property available for all three:

NSString(string: "abcd").uppercased
NSString(string: "ABCD").lowercased
NSString(string: "abCd").capitalized

Language is Hard

The methods above hide a whole string of method delegations to NSString and CFString with a default Locale of nil. This works most of the time. At least, it does in English. The fact is, I don't really understand the rest of what I'm about to paste from my playground.

let turkishI = "\u{0130} is not I"                  // "İ is not I"
turkishI.uppercased()                               // "İ IS NOT I"
turkishI.uppercased(with: Locale(identifier: "en")) // "İ IS NOT I"
turkishI.uppercased(with: Locale(identifier: "tr")) // "İ İS NOT I"
turkishI.lowercased()                               // "i̇ is not i"
turkishI.capitalized                                // "İ Is Not I"
turkishI.lowercased(with: Locale(identifier: "en")) // "i̇ is not i"
turkishI.lowercased(with: Locale(identifier: "tr")) // "i is not ı"

Swift 3

The Locale initializer has a slightly longer parameter name in Swift 3…

turkishI.uppercased(with: Locale(localeIdentifier: "en"))

Light Humor

"✈️".uppercased()  //  (Swift 4)
"✈️".uppercased()  //  (Swift 5)
clozach
  • 5,118
  • 5
  • 41
  • 54
  • the discrepancy regarding uppercased() / lowercased() being functions and capitalized being a property is still present in Swift 5. Odd. – Andreas Grapentin Nov 03 '21 at 03:32
13

The uppercaseString and lowercaseString properties on String are not in the Swift standard library anymore. Instead, Foundation provides them now. So you have to

import Foundation

to use it.

newacct
  • 119,665
  • 29
  • 163
  • 224
0

Heres the function from Apple Docs for Xcode8/Swift3:

/// Returns a lowercase version of the string.
///
/// Here's an example of transforming a string to all lowercase letters.
///
///     let cafe = "Café "
///     print(cafe.lowercased())
///     // Prints "café "
///
/// - Returns: A lowercase copy of the string.
///
/// - Complexity: O(*n*)
public func lowercased() -> String
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
0

SwiftUI Solution

The Solution in SwiftUI to uppercase a string

Text("Hello World!".uppercased())

The output would be: "HELLO WORLD!"


To lowercase the string just use this

Text("Hello World!".lowercased())

The output would be: "hello world!"

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45