字符串和字符

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" //

swift中文文档翻译之--字符串和字符的更多相关文章

  1. Swift语言指南(十)--字符串与字符

    原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...

  2. The Swift Programming Language-官方教程精译Swift(4)字符串和字符

    String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...

  3. Swift入门篇-字符串和字符

    今天主要是介绍一下字符串的用法 ,字符串的语法和object-c语法不太一样,但是思想是一样,就是写法不太一样.如果您对.net和java语法比较熟悉的话,那您几乎没有深压力.如果您对swift 基本 ...

  4. Swift 学习- 04 -- 字符串和字符

    // 字符串 和 字符 // 字符串 是有序的 Character (字符) 类型的值的集合,  通过 String 类型的集合 // swift 的 String 和 Character 类型提供了 ...

  5. [精校版]The Swift Programming Language--语言指南--字符串和字符 (转)

    今天装了10.10.马上就可以实际编写swift了.还是很兴奋啊. 哈哈.字符串和字符是大家最容易打交道的.今天就转一下讲解swift中字符串和字符的文章.希望对大家有帮助. 原文地址:http:// ...

  6. JS判断字符串长度(英文占1个字符,中文汉字占2个字符)

    //计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: String.prototype.gblen = function() { var len = 0; for (var i=0; i ...

  7. 5.Swift教程翻译系列——Swift字符串和字符

    英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 字符串是一组字符的有序序列,比方"hello,china"或 ...

  8. JS判断字符串长度,结合element el-input el-form 表单验证(英文占1个字符,中文汉字占2个字符)

    首先看看判断字符串长度的几种方法(英文占1个字符,中文汉字占2个字符) 方法一: function strlen(str) { var len = 0; for (var i = 0; i < ...

  9. swift(二)swift字符串和字符和逻辑运算

    /* 1.swift字符串和字符 2.构造字符串 3.字符串比较 4.数值运算 5.复制运算 6.关系运算 7.逻辑运算 8.区间运算 */ /* //数据 + 数据的处理 //字符信息+ 字符信息的 ...

随机推荐

  1. 学习笔记——Maven实战(三)多模块项目的POM重构

    重复,还是重复 程序员应该有狗一般的嗅觉,要能嗅到重复这一最常见的坏味道,不管重复披着怎样的外衣,一旦发现,都应该毫不留情地彻底地将其干掉.不要因为POM不是产品代码而纵容重复在这里发酵,例如这样一段 ...

  2. 配置sonar、jenkins进行持续审查

    本文以CentOS操作系统为例介绍Sonar的安装配置,以及如何与Jenkins进行集成,通过pmd-cpd.checkstyle.findbugs等工具对代码进行持续审查. 一.安装配置sonar ...

  3. php文件上传之多文件上传

    在胡说之前,首先声明,本文是建立在掌握php单文件上传的基础上,所以这里就不赘述文件上传服务器配置,表单设置该注意的地方了. 话不多少,直入主题,在请求页面方面有两种写法(只呈现表单部分,以上传三个文 ...

  4. C# Winform实现炫酷的透明动画界面

    做过.NET Winform窗体美化的人应该都很熟悉UpdateLayeredWindow吧,UpdateLayeredWindow可以实现窗体的任意透明,效果很好,不会有毛边.不过使用这个API之后 ...

  5. JAVA中的NIO(一)

    1.IO与NIO IO就是普通的IO,或者说原生的IO.特点:阻塞式.内部无缓冲,面向流. NIO就是NEW IO,比原生的IO要高效.特点:非阻塞.内部有缓存,面向缓冲. 要实现高效的IO操作,尤其 ...

  6. Win2003x64系统

    2K3SP2X64_4IN1.iso 光盘说明:    本光盘由自己制作,包含Win2003 SP2 x64的 4个版本:MSDN SP2 R2 VOL免激活版.惠普OEM版.戴尔OEM版.IBMOE ...

  7. python 参数

    摘自:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00137473844933 ...

  8. 【转】变量的声明和定义,从C到编译原理到C++,再到Java

    基础学了太久,时间一长有些东西就可能记得不太清楚,俗话说得好,"好记性不如烂笔头",所以把基础中的基础-变量的声明和定义,从C到编译原理到C++,再到Java用烂笔头记录下来 最早 ...

  9. bootstrap table简洁扁平的表格

    使用方法 1.在html页面的head标签中引入Bootstrap库(假如你的项目还没使用)和bootstrap-table.css. <link rel="stylesheet&qu ...

  10. 在Ubuntu下安装*.sh

    在Ubuntu下安装*.sh和*.bin的方法 [日期:2009-12-07] 来源:Linux公社  作者:Linux编辑 [字体:大 中 小]   记下在Ubuntu下安装*.sh和*.bin的简 ...