Swift字符串和字符

String是例如"hello, world","albatross"这样的有序的Character(字符)类型的值的集合。通过String类型来表示。 一个String的内容可以用许多方式读取,它包括一个Character值的集合。

字符串字面量
let someString = "Some string literal value"  
初始化空字符串
var emptyString = "" //空字符串字面量  
var anotherEmptyString = String()//初始化方法  
//两个字符串均为空并等价

if emptyString.isEmpty {  
    print("Nothing to see here")
}
字符串可变性
var variableString = "Horse"  
variableString += " and carriage"  
//variableString 现在为 "Horse and carriage"

let constantString = "Highlander"  
//constantString += " and another Highlander" //报错,constantString为常量
字符串是值类型
//Swift 的String类型是值类型。 如果您创建了一个新的字符串,那么当其进行常量、变量赋值操作,或在函数/方法中传递时,会进行值拷贝。 任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值操作。
使用字符
for character in "Dog!".characters {  
    print(character)
}

let exclamationMark: Character = "!"

let catCharacters: [Character] = ["h", "e", "l", "l", "o"]  
let catString = String(catCharacters)  
print(catString)  
连接字符和字符串
let string1 = "hello"  
let string2 = " world"  
var welcome = string1 + string2  
//welcome 现在等于 "hello world"

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

let exclamationMark2: Character? = "!"  
welcome.append(exclamationMark2!)  
print(welcome)  
字符串插值
let multiplier = 3  
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"  
print(message)  
Unicode Scalars
//Unicode 标量
//Swift 的String类型是基于 Unicode 标量 建立的。 Unicode 标量是对应字符或者修饰符的唯一的21位数字,例如U+0061表示小写的拉丁字母`a`
//注意: Unicode 码位(code poing) 的范围是U+0000到U+D7FF或者U+E000到U+10FFFF。Unicode 标量不包括 Unicode 代理项(surrogate pair) 码位,其码位范围是U+D800到U+DFFF。

//字符串字面量特殊字符
//转义字符\0(空字符)、\\(反斜线)、\t(水平制表符)、\n(换行符)、\r(回车符)、\"(双引号)、\'(单引号)。
//Unicode 标量,写成\u{n}(u为小写),其中n为任意一到八位十六进制数且可用的 Unicode 位码。
let eAcute:Character = "\u{E9}"  
let combinedEAcute: Character = "\u{65}\u{301}"  
// eAcute 是 é, combinedEAcute 是 é
print("eAcute is \(eAcute),combinedEAcute is \(combinedEAcute)")

let precomposed: Character = "\u{D55C}" // 한  
let decomposed: Character = "\u{1112}\u{1161}\u{11AB}" // ᄒ, ᅡ, ᆫ  
// precomposed 是 한, decomposed 是 한
print("precomposed is \(precomposed),decomposed is \(decomposed)")

let enclosedEAcute: Character = "\u{E9}\u{20DD}"  
// enclosedEAcute 是 é⃝
print("enclosedEAcute is \(enclosedEAcute)")

let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}"  
// regionalIndicatorForUS 是 [美国国旗]
print("regionalIndicatorForUS is \(regionalIndicatorForUS)")  
计算字符数量
let unusualMenagerie = "hello world"  
print("unusualMenagerie has \(unusualMenagerie.characters.count) characters")

var word = "cafe"  
print("the number of characters in \(word) is \(word.characters.count)")  
word += "\u{301}"  
print("the number of characters in \(word) is \(word.characters.count)")  
访问和修改字符串
//你可以通过字符串的属性和方法来访问和修改它,当然也可以用下标语法完成。

//字符串索引
let greeting = "Guten Tag!"  
greeting[greeting.startIndex] //G  
greeting[greeting.startIndex.successor()] //u  
greeting[greeting.endIndex.predecessor()] //!  
greeting[greeting.startIndex.advancedBy(7)] //a

//遍历
for index in greeting.characters.indices {  
    print("\(greeting[index])", terminator: " ")
}

//插入和删除
var welcome1 = "welcome"  
welcome1.insert("!", atIndex: welcome1.endIndex)  
print(welcome1)

welcome1.insertContentsOf(" three".characters, at: welcome1.endIndex.predecessor())  
print("insertContentsOf: \(welcome1)")

welcome1.removeAtIndex(welcome1.endIndex.predecessor())  
print("removeAtIndex: \(welcome1)")

let range = welcome1.endIndex.advancedBy(-6)..<welcome1.endIndex  
welcome1.removeRange(range)  
print("removeRange: \(welcome1)")  
比较字符串
//Swift 提供了三种方式来比较文本值:字符串字符相等、前缀相等和后缀相等。

//字符串/字符相等
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 consider equal")
}
// 打印输出 "These two strings are considered equal"

let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"  
let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}"  
if eAcuteQuestion == combinedEAcuteQuestion {  
    print("These two strings are considered equal")
}
// 打印输出 "These two strings are considered equal"
//这两个字符群集都是表示字符é的有效方式,所以它们被认为是标准相等的

let latinCapitalLetterA: Character = "\u{41}" //英语中的 A  
let cyrillicCapitalLetterA: Character = "\u{0410}" //俄语中的 A  
if latinCapitalLetterA == cyrillicCapitalLetterA {  
    print("These two characters are not equivalent")
}
// 打印 "These two characters are not equivalent"
//英语中的A跟俄语中的A看着一样,但却有不同的语言意义.

//前缀/后缀相同
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"

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"
字符串的Unicode表现形式
//UTF-8表示
var dogString = "Dog狗[doge]"  
for codeUnit in dogString.utf8 {  
    print("\(codeUnit)", terminator: " ")
}
//UTF-16表示
print("")  
for codeUnit in dogString.utf16 {  
    print("\(codeUnit)", terminator: " ")
}
print("")  
//Unicode标量表示
for codeUnit in dogString.unicodeScalars {  
    print("\(codeUnit)", terminator: " ")
}
print("")  

原文地址,非常感谢翻译者的持续更新.

--EOF--