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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.