String 是有序的字符集合,例如“helloword”
Swift字符串采用String类型来表示,同时它也可以被Character类型的集合所表示。
创建空字符串
<span style="font-size:14px;">var emptyString = ""
var anotherEmptyString = String()</span>
判断 字符串 是否为空 通过检查 isEmpty 属性 返回的 Bool 值来实现
if emptyString.isEmpty{
print("emptyString is empty")
}
//emptyString is empty
可变性
字符串的可变与不可变 取决于 声明的是变量 还是 常量。
var variableString = "Hello"
variableString += "word"
print("variableString is \(variableString)")
variableString is Helloword
let constantString = "Highlander"
constantString += " and another Highlander" //这里会出现编译错误
字符串的长度
字符串的长度可以通过以下方式获取
let msg = "helloWord"
var count = msg.characters.count;
print("msg count is \(count)")
//msg count is 9
字符串的拼接
1,使用加法运算符(+)进行拼接,并创建一个新的字符串
let string1 = "hello"
let string2 = "word"
let string3 = string1 + string2
print("string3 is \(string3)")
let char1 :Character = "A"
let char2 : Character = "B"
let char3 = String (char1) + String(char2)
print("char3 is \(char3)")
print("string1+char1 is \(string1 + String(char1))")
//string3 is helloword
//char3 is AB
//string1+char1 is helloA
在很多教程上看到 String 类型 和 Character 类型,以及两个 Character 类型的变量是直接可以进行 加法运算的,但是我在实际的代码中并没有成功,
所以在进行加法运算的时候,我把所有的Character 类型转换成了String 类型
2,通过加法赋值运算符(+=)将一个字符串或者字符追加到一个已经存在的字符串变量后面:
var string1 = "hello"
var string2 = "swift"
string1 += string2
print("string1 is \(string1)")
var char1:Character = "!"
var welcome = "good morning"
welcome += String(char1)
print("welcome is \(welcome)")
//string1 is helloswift
//welcome is good morning!
注意:不能将一个字符串 或者 字符 追加到一个以存在的 字符变量后面,因为一个字符值只能包含一个字符
字符串的插值
字符串插值是一种全新的构建字符串的方式,可以在其中包含常量、变量、字面量和表达式。你插入字符串字面量的每一项需要包裹在以反斜线为前缀的圆括号中:
//插入变量
var welcome = "welcome to china"
print("you say \(welcome)")
//you say welcome to china
//插入常量
let morning = "good morning"
print("you say \(morning)")
//you say good morning
//插入表达式
let num:Double = 3.1415926
print("\(num) * 125 = \(num * 125) ")
//3.1415926 * 125 = 392.699075
字符串比较
Swift 提供了三种方式来比较字符串的值:字符串相等,前缀相等和后缀相等
字符串相等 使用 == 来比较
var str1 = "this is a dog"
var str2 = "this is a dog"
if str1 == str2{
print("str1 == str2")
}else{
print("str1 != str2")
}
//str1 == str2
判断前缀
通过调用 hasPrefix 函数检查给定的参数字符串是否以 string 为前缀
var str1 = "this is a dog"
var str2 = "that is a cat"
if str1.hasPrefix("this"){
print("str1 hasPrefix this")
}else{
print("str1 not hasPrefix this")
}
if str2.hasPrefix("this"){
print("str2 hasPrefix this")
}else{
print("str2 not hasPrefix this")
}
//str1 hasPrefix this
//str2 not hasPrefix this
判断后缀
通过调用hasSuffix 函数检查给定的参数字符串是否以 string 为后缀
<span style="font-size:14px;">var str1 = "this is a dog"
var str2 = "that is a cat"
if str1.hasSuffix("dog"){
print("str1 hasSuffix dog")
}else{
print("str1 not hasSuffix dog")
}
if str2.hasSuffix("dog"){
print("str2 hasSuffix dog")
}else{
print("str2 not hasSuffix dog")
}
//str1 hasSuffix dog
//str2 not hasSuffix dog</span>
字符串的大小写
通过字符串的 uppercaseString 和 lowercaseString 属性来访问一个字符串的大写或小写的版本。
<span style="font-size:14px;">let normal = "Could you help me, please?"
let shouty = normal.uppercaseString
print("shouty is \(shouty)")
//shouty is COULD YOU HELP ME, PLEASE?
let whispered = normal.lowercaseString
print("whispered is \(whispered)")
//whispered is could you help me, please</span>