swift中文文档翻译之--字符串和字符
字符串和字符
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的String 和Character 类型提供了一种快速,兼容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中文文档翻译之--字符串和字符的更多相关文章
- Swift语言指南(十)--字符串与字符
原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...
- The Swift Programming Language-官方教程精译Swift(4)字符串和字符
String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...
- Swift入门篇-字符串和字符
今天主要是介绍一下字符串的用法 ,字符串的语法和object-c语法不太一样,但是思想是一样,就是写法不太一样.如果您对.net和java语法比较熟悉的话,那您几乎没有深压力.如果您对swift 基本 ...
- Swift 学习- 04 -- 字符串和字符
// 字符串 和 字符 // 字符串 是有序的 Character (字符) 类型的值的集合, 通过 String 类型的集合 // swift 的 String 和 Character 类型提供了 ...
- [精校版]The Swift Programming Language--语言指南--字符串和字符 (转)
今天装了10.10.马上就可以实际编写swift了.还是很兴奋啊. 哈哈.字符串和字符是大家最容易打交道的.今天就转一下讲解swift中字符串和字符的文章.希望对大家有帮助. 原文地址:http:// ...
- JS判断字符串长度(英文占1个字符,中文汉字占2个字符)
//计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: String.prototype.gblen = function() { var len = 0; for (var i=0; i ...
- 5.Swift教程翻译系列——Swift字符串和字符
英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 字符串是一组字符的有序序列,比方"hello,china"或 ...
- JS判断字符串长度,结合element el-input el-form 表单验证(英文占1个字符,中文汉字占2个字符)
首先看看判断字符串长度的几种方法(英文占1个字符,中文汉字占2个字符) 方法一: function strlen(str) { var len = 0; for (var i = 0; i < ...
- swift(二)swift字符串和字符和逻辑运算
/* 1.swift字符串和字符 2.构造字符串 3.字符串比较 4.数值运算 5.复制运算 6.关系运算 7.逻辑运算 8.区间运算 */ /* //数据 + 数据的处理 //字符信息+ 字符信息的 ...
随机推荐
- LeetCode 笔记27 Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- Linux 第一次学习笔记
一.Linux 为何物 Linux 就是一个操作系统,就像你多少已经了解的 Windows(xp,7,8)和 Max OS ,至于操作系统是什么,就不用过多解释了,如果你学习过前面的入门课程,应该会有 ...
- split 方法的正确使用姿势
本文同步自我的个人博客:http://www.52cik.com/2015/11/02/split-skill.html 通过js获取 QueryString (location.search部分) ...
- 关于php析构函数的一个有趣问题
随着面向对象编程的普遍展开,面向对象展现了其中很多有趣的问题.相信很多初学者学习php面向对象时会接触两个函数,构造函数与析构函数.构造函数似乎用的更多,析构函数用的较少(相对初学者有限编程经验而言, ...
- C#中判断一个数组中是否存在某个数组值 及相关
声明:reference:http://www.cnblogs.com/icebutterfly/archive/2010/06/22/1762738.html:http://blog.csdn.ne ...
- 第十一课:js操作选择器的通用函数
1.判断文档是否是XML文档 var isXML = function(elem){ var documentElement = elem && (elem.ownerDocument ...
- 2016 版 Laravel 系列入门教程(五)【最适合中国人的 Laravel 教程】
本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本文是本系列教程的完结篇,我们将一起给 Arti ...
- Java泛型中E、T、K、V等的含义
Java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Numbe ...
- js简单的工厂模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- log4j2 使用说明
因近期需要编写J2EE程序,所以简单学习了Log4j2,这里把我学习的一些信息做记录: 1.从HelloWorld开始 参考:http://logging.apache.org/log4j/2.x/m ...