Swift-字符串和字符

1. 字符串

  • 在 Swift 中 String 类型是值类型。
  • 如果你创建了一个新的字符串,那么当其进行常量、变量赋值操作,或在函数/方法中传递时,会进行值拷贝。
  • 在前述任一情况下,都会对已有字符串值创建新副本,并对该新副本而非原始字符串进行传递或赋值操作

1.1 初始化空字符串

// 两个字符串均为空并等价。
var emptyString = ""	//	空字符串字面量
var anotherEmptyString = String()	//	初始化方法
  • 你可以通过检查 Bool 类型的 isEmpty 属性来判断该字符串是否为空:
if emptyString.isEmpty {
    print("Nothing to see here")
}	//	打印输出:“Nothing to see here”

1.2 字符串可变性

var variableString = "Horse"
variableString += " and carriage"	// variableString 现在为 "Horse and carriage"

let constantString = "Highlander"
constantString += " and another Highlander"	// 这会报告一个编译错误(compile-time error) - 常量字符串不可以被修改。

1.3 连接字符串和字符

  • 字符串可以通过加法运算符(+)相加在一起(或称“连接”)创建一个新的字符串。
  • 可以通过加法赋值运算符(+=)将一个字符串添加到一个已经存在字符串变量上。
  • 可以用 append() 方法将一个字符附加到一个字符串变量的尾部。
let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2	//	welcome 现在等于 "hello there"

var instruction = "look over"
instruction += string2	// instruction 现在等于 "look over there"

let exclamationMark: Character = "!"
welcome.append(exclamationMark)	//	welcome 现在等于 "hello there!"

1.4 字符串插值

let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"	//	"3 times 2.5 is 7.5"

1.5 计算字符数量

let unusualMenagerie = "Koala ?, Snail ?, Penguin ?, Dromedary ?"
print("unusualMenagerie has \(unusualMenagerie.count) characters")
// 打印输出“unusualMenagerie has 40 characters”

1.6 访问和修改字符串

1.6.1 字符串索引

  • 每一个 String 值都有一个关联的索引(index)类型,String.Index,它对应着字符串中的每一个 Character 的位置。
  • 使用 startIndex 属性可以获取一个 String 的第一个 Character 的索引。
  • 使用 endIndex 属性可以获取最后一个 Character 的后一个位置的索引。
  • 如果 String 是空串,startIndex 和 endIndex 是相等的。
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a

通过调用 String 的 index(before:) 或 index(after:) 方法,可以立即得到前面或后面的一个索引。
你还可以通过调用 index(_:offsetBy:) 方法来获取对应偏移量的索引,这种方式可以避免多次调用 index(before:) 或 index(after:) 方法。

  • 使用 indices 属性会创建一个包含全部索引的范围(Range),用来在一个字符串中访问单个字符。
for index in greeting.indices {
   print("\(greeting[index]) ", terminator:" ")
}	//	打印输出“G u t e n   T a g ! ”

注意:
你可以使用 startIndex 和 endIndex 属性或者 index(before:) 、index(after:) 和 index(_:offsetBy:) 方法在任意一个确认的并遵循 Collection 协议的类型里面,如上文所示是使用在 String 中,你也可以使用在 Array、Dictionary 和 Set 中。

1.6.2 插入和删除

  • 调用 insert(_:at:) 方法可以在一个字符串的指定索引插入一个字符。
  • 调用 insert(contentsOf:at:) 方法可以在一个字符串的指定索引插入一个段字符串。
var welcome = "hello"
welcome.insert("!", at: welcome.endIndex)	// welcome 变量现在等于 "hello!"

welcome.insert(contentsOf:" there", at: welcome.index(before: welcome.endIndex))	// welcome 变量现在等于 "hello there!"
  • 调用 remove(at:) 方法可以在一个字符串的指定索引删除一个字符。
  • 调用 removeSubrange(_? 方法可以在一个字符串的指定索引删除一个子字符串。
welcome.remove(at: welcome.index(before: welcome.endIndex))	// welcome 现在等于 "hello there"

let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)	// welcome 现在等于 "hello"

注意:
你可以使用 insert(:at:)、insert(contentsOf:at:)、remove(at:) 和 removeSubrange(? 方法在任意一个确认的并遵循 RangeReplaceableCollection 协议的类型里面,如上文所示是使用在 String 中,你也可以使用在 Array、Dictionary 和 Set 中。

1.7 子字符串

  • 当你从字符串中获取一个子字符串,可以使用下标或者 prefix(_? 之类的方法,就可以得到一个 SubString 的实例,而非另外一个 String。
  • Swift 里的 SubString 绝大部分函数都跟 String 一样,意味着你可以使用同样的方式去操作 SubString 和 String。
  • 跟 String 不同的是,你只有在短时间内需要操作字符串时,才会使用 SubString。
  • 当你需要长时间保存结果时,就把 SubString 转化为 String 的实例。(因为它重用了原 String 的内存空间,原 String 的内存空间必须保留直到它的 SubString 不再被使用为止。)
let greeting = "Hello, world!"
let index = greeting.firstIndex(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning 的值为 "Hello"

// 把结果转化为 String 以便长期存储。
let newString = String(beginning)
String 和 SubString 的区别在于:
	性能优化上,SubString 可以重用原 String 的内存空间,或者另一个 SubString 的内存空间(String 也有同样的优化,但如果两个 String 共享内存的话,它们就会相等)。这一优化意味着你在修改 String 和 SubString 之前都不需要消耗性能去复制内存。就像前面说的那样,SubString 不适合长期存储 —— 因为它重用了原 String 的内存空间,原 String 的内存空间必须保留直到它的 SubString 不再被使用为止。

1.8 字符串比较

Swift 提供了三种方式来比较文本值:

  • 字符串字符相等
  • 前缀相等
  • 后缀相等

1.8.1 字符串/字符相等

  • 使用等于操作符(==)和不等于操作符(!=)
let quotation = "We're a lot alike, you and I."
let sameQuotation = "We're a lot alike, you and I."
if quotation == sameQuotation {
    print("These two strings are considered equal")	// 打印输出“These two strings are considered equal”
}

1.8.2 前缀相等

调用字符串的 hasPrefix(_? 方法来检查字符串是否拥有特定前缀,方法接收一个 String 类型的参数,并返回一个布尔值。

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 += 1
    }
}
print("There are \(act1SceneCount) scenes in Act 1")	//	// 打印输出“There are 5 scenes in Act 1”

1.8.3 后缀相等

调用字符串的 hasSuffix(_? 方法来检查字符串是否拥有特定后缀,方法接收一个 String 类型的参数,并返回一个布尔值。

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 mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
    if scene.hasSuffix("Capulet's mansion") {
        mansionCount += 1
    } else if scene.hasSuffix("Friar Lawrence's cell") {
        cellCount += 1
    }
}
print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")	// 打印输出“6 mansion scenes; 2 cell scenes”

2. 字符

  • 可通过 for-in 循环来遍历字符串,获取字符串中每一个字符的值:
for character in "Dog!" {
    print(character)
}
// D
// o
// g
// !
  • 通过标明一个 Character 类型并用字符字面量进行赋值,可以建立一个独立的字符常量或变量:
let exclamationMark: Character = "!"
  • 字符串可以通过传递一个值类型为 Character 的数组作为自变量来初始化:
let catCharacters: [Character] = ["C", "a", "t", "!"]
let catString = String(catCharacters)
print(catString)	//	打印输出:“Cat!”

3. 字符串的 Unicode 表示形式

当一个 Unicode 字符串被写进文本文件或者其他储存时,字符串中的 Unicode 标量会用 Unicode 定义的几种 编码格式(encoding forms) 编码。每一个字符串中的小块编码都被称 代码单元(code units)

  • UTF-8 编码格式:编码字符串为 8 位的代码单元。
  • UTF-16 编码格式:编码字符串位 16 位的代码单元。
  • UTF-32 编码格式:编码字符串32位的代码单元。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值