String and Characters

A string is a series of characters, such as “hello, world” or “albatross”. Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values.

String Literals

You can include predefined String values within your code as string literals. A string literal is a sequence of characters surrounded by double quotation marks (“).
Use a string literal as an initial value for a constant or variable:

let someString = "Some string literal value"

Multiline String Literals

If you need a string that spans several lines, use a multiline string literal—a sequence of characters surrounded by three double quotation marks:

let quotation = """
The White Rabbit put on his spectacles.  "Where shall I begin,
please your Majesty?" he asked.

"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""

A multiline string literal includes all of the lines between its opening and closing quotation marks. The string begins on the first line after the opening quotation marks (“””) and ends on the line before the closing quotation marks, which means that neither of the strings below start or end with a line break:

let singleLineString = "These are the same."
let multilineString = """
These are the same.
"""

Special Characters in String Literals

String literals can include the following special characters:

  • The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \” (double quotation mark) and \’ (single quotation mark)
  • An arbitrary Unicode scalar value, written as \u{n}, where n is a 1–8 digit hexadecimal number (Unicode is discussed in Unicode below)

   转载规则


《String and Characters》 志鹏 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
Runtime消息转发基础 Runtime消息转发基础
Runtime 的定义 The Objective-C language defers as many decisions as it can from compile time and link time to runtime. When
2019-09-09 志鹏
下一篇 
栈
栈和队列是在程序设计中被广泛使用的的两种线性数据结构,它们的特点在于基本操作的特殊性,栈必须按“后进先出”的规则进行操作,而队列必须按“先进先出”的规则进行操作。和线性表相比,它们的插入和删除操作受更多的约束和限制,故又称限定性的线性表结
2019-09-08 志鹏
  目录