iOS Developer Library — Pre-Release

Developer

Swift Standard Library Reference

PDF
On This Page

String

A String represents an ordered collection of characters.

For a full discussion of String, see Strings and Characters.

Creating a String

init()

Constructs an empty string.

Declaration

  • init()

Discussion

Creating a string using this initializer:

  • let emptyString = String()

is equivalent to using double-quote convenience syntax:

  • let equivalentString = ""

init(count:repeatedValue:)

Constructs a string with a single character or Unicode scalar value repeated a given number of times.

Declaration

  • init(count sz: Int, repeatedValue c: Character)
  • init(count: Int, repeatedValue c: UnicodeScalar)

Discussion

The resulting string contains the supplied repeatedValue character or Unicode scalar value, repeated the specified number of times:

  • let letterA: Character = "a"
  • let string = String(count: 5, repeatedValue: letterA)
  • // string is "aaaaa"
  • let letterB: UnicodeScalar = "\u{0062}"
  • let anotherString = String(count: 5, repeatedValue: letterB)
  • //anotherString is "bbbbb"

Querying a String

isEmpty

A Boolean value that indicates whether the string is empty (read-only).

Declaration

  • var isEmpty: Bool { get }

Discussion

Use this read-only property to query whether the string is empty, which means it has no characters:

  • var string = "Hello, world!"
  • let firstCheck = string.isEmpty
  • // firstCheck is false
  • string = ""
  • let secondCheck = string.isEmpty
  • // secondCheck is true

hasPrefix(_:)

Returns a Boolean value that indicates whether the first characters in the string are the same as the characters in a specified string.

Declaration

  • func hasPrefix(prefix: String) -> Bool

Discussion

Use this method to determine whether a string has a particular prefix:

  • let string = "Hello, world"
  • let firstCheck = string.hasPrefix("Hello")
  • // firstCheck is true
  • let secondCheck = string.hasPrefix("hello")
  • // secondCheck is false

This method perfoms a character-by-character canonical equivalence comparision between the Unicode extended grapheme clusters in each string.

hasSuffix(_:)

Returns a Boolean value that indicates whether the last characters in the string are the same as the characters in a specified string.

Declaration

  • func hasSuffix(suffix: String) -> Bool

Discussion

Use this method to determine whether a string has a particular suffix:

  • let string = "Hello, world"
  • let firstCheck = string.hasSuffix("world")
  • // firstCheck is true
  • let secondCheck = string.hasSuffix("World")
  • // secondCheck is false

This method perfoms a character-by-character canonical equivalence comparision between the Unicode extended grapheme clusters in each string.

Converting Strings

toInt()

Returns an optional integer that contains the result of attempting to convert the characters in the string to an integer value.

Declaration

  • func toInt() -> Int?

Discussion

Use this method to convert a string to an integer value. The method returns an optional integer value (Int?)—if the conversion succeeds, the value is the resulting integer; if the conversion fails, the value is nil:

  • let string = "42"
  • if let number = string.toInt() {
  • println("Got the number: \(number)")
  • } else {
  • println("Couldn't convert to a number")
  • }
  • // prints "Got the number: 42"

Operators

+

Returns a string that contains the result of concatenating two strings.

Declaration

  • func +(lhs: String, rhs: String) -> String

Discussion

Use the + operator to concatenate two strings:

  • let combination = "Hello " + "world"
  • // combination is "Hello world"

If the value supplied on the left-hand side of the operator is an empty string, the resultant value is the unmodified value on the right-hand side.

+=

Appends a string to an existing string.

Declaration

  • func +=(inout lhs: String, rhs: String)

Discussion

Use the += operator to append a string to the end of an existing string:

  • var string = "Hello "
  • string += "world!"
  • // string is "Hello world!"

If the initial string is empty, the resultant value is the unmodified rhs value.

You can only use the += operator to append a string to an existing string if you declared the existing string using the var keyword (that is, as a variable and not a constant):

  • let string = "Hello "
  • string += "world!"
  • // Error: cannot mutate a constant string

==

Returns a Boolean value that indicates whether the contents of two strings are the same.

Declaration

  • func ==(lhs: String, rhs: String) -> Bool

Discussion

Evaluates to true if the two string values contain exactly the same characters in exactly the same order:

  • let string1 = "Hello world!"
  • let string2 = "Hello" + " " + "world" + "!"
  • let result = string1 == string2
  • // result is true

This method perfoms a character-by-character canonical equivalence comparision between the Unicode extended grapheme clusters in each string.

<

Returns a Boolean value that indicates whether one string lexicographically precedes another.

Declaration

  • func <(lhs: String, rhs: String) -> Bool

Discussion

Evaluates to true if the lhs value is less than the rhs value, by performing a lexicographical, locale-insensitive comparison of the characters:

  • let string1 = "Number 3"
  • let string2 = "Number 2"
  • let result1 = string1 < string2
  • // result1 is false
  • let result2 = string2 < string1
  • // result2 is true

This operation is locale-insensitive. As a result, if you are comparing strings to present to the end-user, you should typically use one of the locale-sensitive comparison methods of the NSString class instead. For example, see the compare:options:range:locale:, localizedCompare:, and localizedCaseInsensitiveCompare: methods of the NSString class.