参考书:
The Swift Programming Language
The Swift Programming Language 中文版
软件版本:
OS X Yosemite 10.10.3
Xcode 6.3.2
Swift 1.2
标签:
Swift
编程学习笔记
字符串和字符(Strings and Characters)
字符串字面量(String Literals)
- 特殊字符
1.1 空字符:\0
1.2反斜杠:\
1.3 水平制表符:\t
1.4 换行符:\n
1.5 回车符:\r
1.6 双引号:"
1.7 单引号:'
1.8 Unicode标量:\u{n},n为任意的一到八位十六进制数
println("\0")
println("\\")
println("\t")
println("a\n")
println("a\r")
println("\"")
println("\'")
println("\u{1F496}") //1F496,即一个十六进制数
代码示例
初始化空字符串(Initializing an Empty String)
- 给变量附上空值
var emptyString = ""
- 初始化一个
String
实例
var anotherEmptyString = String()
-
String
类中,有isEmpty
属性,可以判断字符串是否为空,isEmpty
为Boolean
类型
字符串可变性(String Mutability)
字符串是值类型(Strings Are Value Types)
使用字符(Working with Characters)
- 声明时标注一个
Character
类型,可以建立一个独立的字符常量或者变量
let yenSign:Character = "¥"
计算字符数量(Counting Characters)
1.调用全局count()
函数
let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"
println("unusualMenagerie has \(count(unusualMenagerie)) characters")
计算 unusualMenagerie 中的字符数量
连接字符串和字符(Concatenating Strings and Characters)
- 用
+
运算对字符串进行拼接
let string1 = "Hello"
let string2 = " there"
var welcome = string1 + string2
- 用
+=
将一个字符串添加到一个已经存在的字符串变量上
var instruction = "look over"
instruction += string2
- 用
append()
方法将一个字符添加到字符串变量的尾部
let exclamationMark:Character = "!"
welcome.append(exclamationMark)
比较字符串(Comparing Strings)
- 字符串相等,
==
直接判断
let quotation = "我们是一样一样滴."
let sameQuotation = "我们是一样一样滴."
if quotation == sameQuotation {
println("这两个字符串被认为是相同的")
}
- 前缀/后缀相等,调用字符串的
hasPrefix()
或者hasSuffix()
方法
let romeoAndJuliet = [
"Act 1 Scene 1: Verona, A public place",
"Act 1 Scene 2: Capulet's mansion",
"Act 1 Scene 3: A room in Capulet's mansion",
"Act 1 Scene 4: A street outside Capulet's mansion",
"Act 1 Scene 5: The Great Hall in Capulet's mansion",
"Act 2 Scene 1: Outside Capulet's mansion",
"Act 2 Scene 2: Capulet's orchard",
"Act 2 Scene 3: Outside Friar Lawrence's cell",
"Act 2 Scene 4: A street in Verona",
"Act 2 Scene 5: Capulet's mansion",
"Act 2 Scene 6: Friar Lawrence's cell"
]
var act1SceneCount = 0
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1 ") {
++act1SceneCount
}
}
println("There are \(act1SceneCount) scenes in Act 1")
var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
if scene.hasSuffix("Capulet's mansion") {
++mansionCount
} else if scene.hasSuffix("Friar Lawrence's cell") {
++cellCount
}
}
大写和小写字符串(Uppercase and Lowercase Strings)
- 调用字符串的
uppercaseString
或者lowercaseString
属性实现
import Foundation
let normal = "Could you help me, please?"
let shouty = normal.uppercaseString
let whispered = normal.lowercaseString
Unicode
Unicode 中每一个字符都可以被解释为一个或多个 unicode 标量。 字符的 unicode 标量是一个唯一的21位数字(和名称),例如U+0061表示小写的拉丁字母A ("a"),U+1F425表示小鸡表情 ("🐥")
Swift能够以三种(?)Unicode兼容的方式访问字符串的值:
- UTF-8
for codeUnit in dogString.utf8 {
print("\(codeUnit) ")
}
print("\n")
- UTF-16
for codeUnit in dogString.utf16 {
print("\(codeUnit) ")
}
print("\n")
// 68 111 103 33 55357 56374
- Unicode 标量 (Unicode Scalars)
for scalar in dogString.unicodeScalars {
print("\(scalar.value) ")
}
print("\n")
网友评论