Strings and Characters
与大多数语言一致(本人只学过少数几门语言,所以不以一概全),一个String字符串其实是一个有序的characters字符集合.在Swif中,字符串又String类型代表,也就是说,一个字符串又一个Character类型的集合值表示.
-
String Literals(字符串字面量)
字符串的字面量被用来初始化一个变量或者一个常量.例如:
let someString = "Some string literal value"
Note:Swift能够自动推断这个someString常量是一个String类型,因为他被一个String的字面量初始化.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)
//转义字符,\0(空字符 ""),\\(反斜杠"\"),\t(制表符),\n(换行符),\r(回车),\"(双引号""")和\'(单引号"'") •Single-byte Unicode scalars, written as \xnn, where nn is two hexadecimal digits
//单字节的Unicode标量,写作\xnn,这里的nn是2个16字节数字
•Two-byte Unicode scalars, written as \unnnn, where nnnn is four hexadecimal digits
//双字节的Unicode标量,写作\unnnn,这里的nnnn是4个16字节的数字
•Four-byte Unicode scalars, written as \Unnnnnnnn, where nnnnnnnn is eight hexadecimal digits
//4字节Unicode标量,写作\Unnnnnnnn,这里的nnnnnnnn是8个16字节的数字
下面给出一个例子说明下这个问题:
1 let wiseWords = "\"Imagination is more important than knowledge\" - Einstein" 2 // "Imagination is more important than knowledge" - Einstein 3 let dollarSign = "\x24" // $, Unicode scalar U+0024 4 let blackHeart = "\u2665" // ♥, Unicode scalar U+2665 5 let sparklingHeart = "\U0001F496" // , Unicode scalar U+1F496(这里其实不是 什么都没有,是因为字体不支持这个unicode,所以无法显示.在MAC系统设备上是能正确看到的)
-
Initializing an Empty String(初始化一个空字符串)
To create an empty String value as the starting point for building a longer string, either assign an empty string literal to a variable, or initialize a new Stringinstance with initializer syntax:
//创建一个空的字符串来构建一个更长的字符串,或者给一个变量赋上一个空的字符串字面量,或者使用初始化字符串的语法来创建.
在JAVA中可以使用一下办法来创建一个空字符串的String
String str = ""; String str = new String();
当然在Swift中也可以:
var str = "" //使用空字符串的字面量 var str = String()//使用初始化语法
Note:与java不同,swift创建一个String()一个对象不需使用new.
现在我们创建出了一个空字符串的String变量,那么我们如何来检查一个String的值是否是一个空字符串呢,在Swift中String类型提供了一个isEmpty属性,返回一个Boolean值
var str = ""
if str.isEmpty{ println("Nothing to see here") }// prints "Nothing to see here"
-
String Mutability(字符串的可变性)
你可以通过改变String类型的值来证明String的可变性,当然这个必须是变量,而不是常量.例如:
var str = "hello" str += " world" //str is now "hello world" let constantString = "hello" str += "world" //this reports conpile-time err - a constant string cannot be modified
很明显用let修饰的constantString是常量,编译时就会给出错误:无法对一个常量进行修改.
-
Strings Are Value Types(字符串是值类型)
String是值类型,表明在使用一个字符串的时候,其实把这个字符串的值拷贝了一份,用拷贝的一份来做操作.而不是原来的那一个String了.这一点很多语言都一样,就不在多说了
-
Working with Characters(使用字符)
文档说到:字符串是由一个指定饿字符集表示的,而每一个字符是又一个unicode字符组成
这里我们使用了foreach循环.后续会讲到.这里就不多讲了.Swift’s String type represents a collection of Character values in a specified order. Each Character value represents a single Unicode character. You can access the individual Character values in a string by iterating over that string with a for-in loop: 1 for character in "Dog! " { 2 println(character) 3 } 4 // D 5 // o 6 // g 7 // ! 8 //
如何创建一个字符:
这里我们只能写一个字符"y",而不能是2个字符"yy".let yenSign:Character = "y"
-
counting Characters(字符计数)
检索一个字符串中有多少个字符,怎么做呢?Swift开发者们给我吗提供一个全局函数 countElements ,你要做的仅仅是传递唯一一个String类型的参数.例如:
说到这个 countElements,作者告诉我们在NSString中,length与countElements不同,虽然字面上的意思都是获取个数,但是Length是基于16-bit code units,UTF-16表示法的,与Unicode字符不同.所以2个方法不一样.(英语不是太好,可能理解有误)let unusualMenagerie = "Koala , Snail , Penguin , Dromedary " println("unusualMenagerie has \(countElements(unusualMenagerie)) characters") // prints "unusualMenagerie has 40 characters"
-