字符串和字符

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. [iOS翻译]《iOS 7 Programming Cookbook》:iOS文件与文件夹管理(下)

    三. 创建文件夹 问题: 你想创建文件夹到磁盘,存储一些文件到里面 解决方案: 使NSFileManager类的实例方法createDirectoryAtPath:withIntermediateDi ...

  2. 网络最大流问题之Ford-Fulkerson算法原理详解

    前言 最大流问题是网络优化中典型的问题,用形象的语言来描述就是在满足容量约束的前提下将尽可能多的流从源节点(始点)到汇节点(终点).解决此问题的经典方法很多,本文介绍广为人熟知的Ford-Fulker ...

  3. Git差异比对

    一. 查看变更还未载入(changed but unstaged,当前没有add 的内容)的文件比对: 只需运行不带任何参数的'git diff'命令即可 二. 查看载入(stage,即已经add)而 ...

  4. 学习笔记——Maven 命令行选项

    2014-10-09:更新裁剪反应堆具体用法 说明: 1.使用-选项时,和后面的参数之间可以不要空格.而使用--选项时,和后面的参数之    间必须有空格.如下面的例子: $ mvn help:des ...

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

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

  6. ROM存储1/4周期正弦信号构造DDS

    上周的时候,老师让编写一个简单的dds程序,本文说明了整个过程中我遇到问题以及一些个人的思考.初次接触FPGA,如有问题请多多指教~ 1.几个疑问,解决和没有解决的. 为何采用ROM而不是直接采用DD ...

  7. 记录我开始学习 Git的路程

    工作半年多了,总觉得没学到什么东西,于是乎找了个Git学习一下,感觉还蛮厉害的样子.为此记录下我的路程 2015,11,26 更新 前面的路都挺艰难的,在官网下载msysgit网速几乎为0(心情千万只 ...

  8. UEditor编辑文章出现多余空行问题的解决办法

    网站后台管理编辑文章以后,保存.再次打开编辑是文章前后多出多余的空行.一直以为是编辑器有问题,也没在意,但是自己编辑文章多了,感觉太麻烦了.想一定可以解决这个问题.百度一下果然找到原因.原来是加载编辑 ...

  9. sql脚本比较大,sqlserver 无法导入,就用cmd命令执行

    osql简单用法:用来将本地脚本执行,适合sql脚本比较大点的情况,执行起来比较方便 1 osql -S serverIP -U sa -P 123 -i C:\script.sql serverIP ...

  10. 开放封闭原则(OCP,Open Closed Principle)

    tks:http://www.cnblogs.com/Benjamin/p/3251987.html