Swift - String, Character

來源:互聯網
上載者:User

標籤:其他   整數   document   斜杠   put   output   rac   string   char   

String, Character

String 在 Swift 中使用雙引號(”)作為界限:

let greeting = "Hello!"  // greeting‘s type is String

Character 可以從string初始化,只要只包含一個字元:

let chr: Character = "H" // validlet chr2: Character = "??" // validlet chr3: Character = "abc" // invalid - multiple grapheme clusters
字串插入值

String interpolation 允許注入一個運算式,可以是任何值,包括字串,整數,浮點數和其他各種。

文法是用一個斜杠,後面緊跟著一對括弧括起來的值:\(value)。任何有效運算式都可以出現在括弧裡,包括函數調用。

let number = 5let interpolatedNumber = "\(number)"  // string is "5"let fortyTwo = "\(6 * 7)"             // string is "42"let example = "This post has \(number) view\(number == 1 ? "" : "s")"// It will output "This post has 5 views" for the above example.// If the variable number had the value 1, it would output "This post has 1 view" instead.
特殊的字元
  • \0
    Null 字元
  • \
    斜杠
  • \t
    tab符
  • \v
    縱向tab
  • \r
    斷行符號符
  • \n
    分行符號
  • \”
    雙引號
  • \’
    單引號
  • \u{n}
    Unicode 符號

比如:

let message = "Then he said, \"I \u{1F496} you!\""print(message) // Then he said, "I ?? you!"

Swift - String, Character

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.