Strings and Characters - seamanhzhang/Swift GitHub Wiki

  • A string literal is a fixed sequence of textual characters surrounded by a pair of double quotes ("").

    let someString = "Some string literal value"

  • String in Swift are Value Type.

  • String values can be added together (or concatenated) with the audition operator (+) to create a new String value.

  • String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.

    let multiplier = 3

    let message = "(multiplier) times 2.5 is (Double(multiplier) * 2.5)"

    // message is "3 times 2.5 is 7.5"

  • String literals can include the following special characters:

    • The escaped special characters \0(null character), \\(backslash), \t(horizontal tab), \n(line feed), "(double quote) and '(single quote)

    • An arbitrary Unicode scalar, written as \u{n}, where n is a 1-8 digit hexadecimal number with a value equal to a valid Unicode code point