标签:des style blog http color 使用
注意:Swift 的 String 类型与 Foundation NSString 类进行了无缝桥接。如果您利用 Cocoa 或 Cocoa Touch 中的 Foundation 框架进行工作,整个 NSString API 都可以调用您创建的任意 String 类型的值,除了本章介绍的String特性。您也可以在任意要求传入NSString 实例作为参数的 API 中使用 String 类型的值进行替换。 |
注意:someString 常量通过字符串字面量进行初始化,Swift 因此推断其为 String 类型。 |
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
1 var emptyString = "" // empty string literal 2 var anotherEmptyString = String() // initializer syntax 3 // 这两个字符串都为空,并且两者等价
1 if emptyString.isEmpty { 2 println("Nothing to see here") 3 } 4 // 打印 "Nothing to see here"
1 var variableString = "Horse" 2 variableString += " and carriage" 3 // variableString 现在为 "Horse and carriage" 4 let constantString = "Highlander" 5 constantString += " and another Highlander" 6 // 这会报告一个编译错误(compile-time error) - 常量不可以被修改。
注意:在 Objective-C 和 Cocoa 中,您通过选择两个不同的类( NSString 和 NSMutableString )来指定该字符串是否可以被修改,Swift中的字符串是否可以修改仅通过定义的是变量还是常量来决定,实现了多种类型可变性操作的统一。 |
注意:和Cocoa 中的 NSString 不同,当您在 Cocoa 中创建了一个 NSString 实例,并将其传递给一个函数/方法,或者赋给一个变量,您永远都是传递或赋值同一个 NSString 实例的一个引用。除非您特别要求其进行值拷贝,否则字符串不会进行赋值新副本操作。 |
1 for character in "Dog!????" { 2 println(character) 3 } 4 // D 5 // o 6 // g 7 // ! 8 // ????
1 let unusualMenagerie = "Koala ????, Snail ????, Penguin ????, Dromedary ????" 2 println("unusualMenagerie has \(countElements(unusualMenagerie)) characters") 3 // prints "unusualMenagerie has 40 characters"
注意:
1.不同的 Unicode 字符以及相同 Unicode 字符的不同表示方式可能需要不同数量的内存空间来存储,所以Swift 中的字符在一个字符串中表示并不一定占用相同的内存空间。因此,字符串的长度不得不通过迭代字符串中每一个字符的长度来进行计算。如果您正在处理一个长字符串,需要注意 countElements 函数必须遍历字符串中的字符,以精准计算字符串的长度。
2.另外需要注意的是通过 countElements 返回的字符数量并不总是与包含相同字符的 NSString 的 length 属性相同。NSString 的 length 属性是基于利用 UTF-16 表示的十六位code units数目,而不是基于 Unicode 字符。为了解决这个问题,NSString 的 length 属性在被 Swift的 String值访问时会被称为utf16count。
|
1 let string1 = "hello" 2 let string2 = " there" 3 let character1: Character = "!" 4 let character2: Character = "?" 5 6 let stringPlusCharacter = string1 + character1 // 等于 "hello!" 7 let stringPlusString = string1 + string2 // 等于 "hello there" 8 let characterPlusString = character1 + string1 // 等于 "!hello" 9 let characterPlusCharacter = character1 + character2 // 等于 "!?"
1 var instruction = "look over" 2 instruction += string2 3 // instruction 现在等于 "look over there" 4 5 var welcome = "good morning" 6 welcome += character1 7 // welcome 现在等于 "good morning!"
注意:您不能将一个字符串或者字符添加到一个已经存在的字符变量上,因为字符变量只能包含一个字符。 |
1 let multiplier = 3 2 let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)" 3 // message is "3 times 2.5 is 7.5"
注意:您插值字符串中写在括号中的表达式不能包含非转义双引号 (") 和反斜杠 (\),并且不能包含回车或换行符。 |
1 let quotation = "We‘re a lot alike, you and I." 2 let sameQuotation = "We‘re a lot alike, you and I." 3 if quotation == sameQuotation { 4 println("These two strings are considered equal") 5 } 6 // prints "These two strings are considered equal"
1 let romeoAndJuliet = [ 2 "Act 1 Scene 1: Verona, A public place", 3 "Act 1 Scene 2: Capulet‘s mansion", 4 "Act 1 Scene 3: A room in Capulet‘s mansion", 5 "Act 1 Scene 4: A street outside Capulet‘s mansion", 6 "Act 1 Scene 5: The Great Hall in Capulet‘s mansion", 7 "Act 2 Scene 1: Outside Capulet‘s mansion", 8 "Act 2 Scene 2: Capulet‘s orchard", 9 "Act 2 Scene 3: Outside Friar Lawrence‘s cell", 10 "Act 2 Scene 4: A street in Verona", 11 "Act 2 Scene 5: Capulet‘s mansion", 12 "Act 2 Scene 6: Friar Lawrence‘s cell" 13 ]
1 var act1SceneCount = 0 2 for scene in romeoAndJuliet { 3 if scene.hasPrefix("Act 1 ") { 4 ++act1SceneCount 5 } 6 } 7 println("There are \(act1SceneCount) scenes in Act 1") 8 // prints "There are 5 scenes in Act 1"
1 var mansionCount = 0 2 var cellCount = 0 3 for scene in romeoAndJuliet { 4 if scene.hasSuffix("Capulet‘s mansion") { 5 ++mansionCount 6 } else if scene.hasSuffix("Friar Lawrence‘s cell") { 7 ++cellCount 8 } 9 } 10 println("\(mansionCount) mansion scenes; \(cellCount) cell scenes") 11 // prints "6 mansion scenes; 2 cell scenes"
1 let normal = "Could you help me, please?" 2 let shouty = normal.uppercaseString 3 // shouty 值为 "COULD YOU HELP ME, PLEASE?" 4 let whispered = normal.lowercaseString 5 // whispered 值为 "could you help me, please?"
1 let dogString = "Dog!????"
1 for codeUnit in dogString.utf8 { 2 print("\(codeUnit) ") 3 } 4 print("\n") 5 // 68 111 103 33 240 159 144 182
1 for codeUnit in dogString.utf16 { 2 print("\(codeUnit) ") 3 } 4 print("\n") 5 // 68 111 103 33 55357 56374
1 for scalar in dogString.unicodeScalars { 2 print("\(scalar.value) ") 3 } 4 print("\n") 5 // 68 111 103 33 128054
1 for scalar in dogString.unicodeScalars { 2 println("\(scalar) ") 3 } 4 // D 5 // o 6 // g 7 // ! 8 // ????
The Swift Programming Language-官方教程精译Swift(4)字符串和字符,布布扣,bubuko.com
The Swift Programming Language-官方教程精译Swift(4)字符串和字符
标签:des style blog http color 使用
原文地址:http://www.cnblogs.com/myios/p/3857902.html