字符串和字符

A string is an ordered collection of characters, such as "hello, world" or "albatross". Swift strings are represented by the String type, which in turn represents a collection of values of Character type.

Swift’s String and Character types provide a fast, Unicode-compliant way to work with text in your code. The syntax for string creation and manipulation is lightweight and readable, with a similar syntax to C strings. String concatenation is as simple as adding together two strings with the + operator, and string mutability is managed by choosing between a constant or a variable, just like any other value in Swift.

字符串是一串有序的字符集合,如“hello, world”或“albatross”。Swift 字符串是由String类型表示,这又代表字符类型的值的集合。

Swift的StringCharacter 类型提供了一种快速,兼容Unicode的方式来在你的代码中与文本工作。字符串的创建和操作语法是简便和易读的 ,与C语言的字符串有着类似的语法。字符串连接很简单的,只要使用+运算符把两个字符串相加即可,字符串的可变性由常量或变量之间进行选择来管理,就像在Swift中任何的其他值。

Despite this simplicity of syntax, Swift’s String type is a fast, modern string implementation. Every string is composed of encoding-independent Unicode characters, and provides support for accessing those characters in various Unicode representations.

Strings can also be used to insert constants, variables, literals, and expressions into longer strings, in a process known as string interpolation. This makes it easy to create custom string values for display, storage, and printing.

Note

Swift’s String type is bridged seamlessly to Foundation’s NSString class. If you are working with the Foundation framework in Cocoa or Cocoa Touch, the entire NSString API is available to call on any String value you create, in addition to the String features described in this chapter. You can also use a String value with any API that requires an NSString instance.

For more information about using String with Foundation and Cocoa, see Using Swift with Cocoa and Objective-C.

String Literals

字符串字面值

You can include predefined String values within your code as string literals. A string literal is a fixed sequence of textual characters surrounded by a pair of double quotes ("").

A string literal can be used to provide an initial value for a constant or variable:在你的代码中你可以在字符串字面值中包含预定义的字符串值。字符串面值是由一对双引号括(“”)起来的文本字符的固定顺序。

字符串面值可以被用来提供一个常量或变量的初始值:

  •     let someString = "Some string literal value"

Note that Swift infers a type of String for the someString constant, because it is initialized with a string literal value.

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 quote) and \' (single quote)

    Single-byte Unicode scalars, written as \xnn, where nn is two hexadecimal digits

    Two-byte Unicode scalars, written as \unnnn, where nnnn is four hexadecimal digits

    Four-byte Unicode scalars, written as \Unnnnnnnn, where nnnnnnnn is eight hexadecimal digits

The code below shows an example of each kind of special character. The wiseWords constant contains two escaped double quote characters. The dollarSign, blackHeart, and sparklingHeart constants demonstrate the three different Unicode scalar character formats:

    let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// "Imagination is more important than knowledge" - Einstein
let dollarSign = "\x24" // $, Unicode scalar U+0024
let blackHeart = "\u2665" // ♥, Unicode scalar U+2665
let sparklingHeart = "\U0001F496" //

最新文章

  1. 【转】bind - DNS 設定
  2. Linux下Kafka单机安装配置方法(图文)
  3. C++ ORM ODB 入门介绍(一)
  4. Linux服务器集群技术的概述
  5. Jquery autocomplete 插件使用
  6. JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象
  7. List list = new ArrayList()
  8. Java 多线程详解(二)------如何创建进程和线程
  9. 洛谷 [P3110] 驮运
  10. Java进阶——带你入门分布式中的Nginx
  11. golang与python多线程的并发速度
  12. 利用python操作excel
  13. 在flask中使用websocket-实时消息推送
  14. Android开发——JobScheduler机制
  15. maven正确的集成命令-U-B
  16. TeeChart缩放
  17. volatile关键字的作用、原理
  18. 已经菜到不行了 PAT 1010. Radix (25)
  19. 【codeforces】【比赛题解】#864 CF Round #436 (Div.2)
  20. linux查看目录的四种方法(ls只显示目录)【转】

热门文章

  1. AWS S3使用小结
  2. 20135202闫佳歆--week 7 深入理解计算机系统第七章--读书笔记
  3. survival analysis 生存分析与R 语言示例 入门篇
  4. MVC5 + EF6 + Bootstrap3 (14) 分部视图PartialView
  5. 收藏所用C#技术类面试、笔试题汇总
  6. 【niubi-job——一个分布式的任务调度框架】----如何开发一个niubi-job的定时任务
  7. ArcGIS中的北京54和西安80投影坐标系详解
  8. express的session函数
  9. 【BZOJ1011】【HNOI2008】遥远的行星(乱搞)
  10. am,pm时间转换