Swift5.0 字符串(String)详解

1.字符串拼接

  • + 拼接
let str1 = "李连杰"
let str2 = "成龙"
let str3 = str1 + str2
print(str3)
// 李连杰成龙
  • \() 拼接
let str1 = "李连杰"
let str2 = "成龙"
let str3 = "\(str1)" + "\(str2)"
print(str3)
// 李连杰成龙

2.字符串是否为空判断

let str1 = "李连杰"
let reult: Bool = str1.isEmpty
print(reult)
// false

let str1 = ""
let reult: Bool = str1.isEmpty
print(reult)
// true

3.字符串长度

let str1 = "李连杰"
let str2 = "成龙"
let str3 = "\(str1)" + "\(str2)"
let count: Int = str3.count
print(count)
// 5

4.字符串比较

  • ==
let str1 = "李连杰"
let str2 = "成龙"
let result: Bool = str1 == str2
print(result)
// false

let str1 = "李连杰"
let str2 = "李连杰"
let result: Bool = str1 == str2
print(result)
// true
  • >
let str1 = "李连杰"
let str2 = "成龙"
let result: Bool = str1 > str2
print(result)
// true
  • <
let str1 = "李连杰"
let str2 = "成龙"
let result: Bool = str1 < str2
print(result)
// false

5.字符串分割

  • 分割成数组
let str = "Hello World Swift!" // 创建字符串
let result = str.components(separatedBy: " ")
print(result)
// ["Hello", "World", "Swift!"]
  • 获取字符
let str = "Hello World Swift!" // 创建字符串
let result = str[str.startIndex] // 第一个字符(Character)
print(result)
// H

let str = "Hello World Swift!" // 创建字符串
let result = str[str.index(str.startIndex, offsetBy: 4)]
print(result)
// o

let str = "Hello World Swift!" // 创建字符串
let result = str.prefix(4) // 从前面截取字符串
print(result)
// Hell

let str = "Hello World Swift!" // 创建字符串
let result = str.suffix(4) // 从后面截取字符串
print(result)
// ift!

// 前面截取区间
let str = "Hello World Swift!" // 创建字符串
let sub1 = str.index(str.startIndex, offsetBy: 3)
let sub2 = str.index(str.startIndex, offsetBy: 6)
let result = str[sub1...sub2]
print(result)
// lo W

// 取sub0之后的所有字符串
let str = "Hello World Swift!" // 创建字符串
let sub0 = str.index(str.startIndex, offsetBy: 3)
let sub1 = str.suffix(from: sub0)
print(sub1)
// lo World Swift!
  • 字符串替换
let str = "Hello World Swift!" // 创建字符串
guard let rang = Range(NSRange(location: 0, length: 3), in: str) else { return }
let sub1 = str.replacingCharacters(in: rang, with: "LZYY")
print(sub1)
// LZYYlo World Swift!
  • 字符串去除前后空格
let str = "  我 是谁  "
let sub1 = str.trimmingCharacters(in: CharacterSet.whitespaces)
print(sub1)
// 我 是谁
  • 删除指定的前后字符
//原始字符串
let str1 = "<<骆驼<>祥子>>"
let characterSet = CharacterSet(charactersIn: "<>")
let sub1 = str1.trimmingCharacters(in: characterSet)
print(sub1)
// 骆驼<>祥子
  • 判断字符串的前缀和后缀
// 前缀
let str = "Hello World Swift!" // 创建字符串
let sub1 = str.hasPrefix("Hell")
print(sub1)
// true

// 后缀
let str = "Hello World Swift!" // 创建字符串
let sub1 = str.hasSuffix("ft!")
print(sub1)
// true
  • 字符串大小写转换
// 转为大写
var str:String = "av,1,2,3,4,5,6,7,8,a,b,c,d,e"
let str1 = str.uppercased()
print(str1)
// AV,1,2,3,4,5,6,7,8,A,B,C,D,E


// 转为小写
var str:String = "AV,1,2,3,4,5,6,7,8,A,B,C,D,E"
let str1 = str.lowercased()
print(str1)
// av,1,2,3,4,5,6,7,8,a,b,c,d,e

//每个单词首字母大写
var str:String = "av,1,2,3,4,5,6,7,8,adfvf,bdsf,cdsf,ddf,e"
let str1 = str.capitalized
print(str1)
// Av,1,2,3,4,5,6,7,8,Adfvf,Bdsf,Cdsf,Ddf,E
  • 包含
// 是否包含某字符串
var str:String = "av,1,2,3,4,5,6,7,8,adfvf,bdsf,cdsf,ddf,e"
let str1 = str.contains("adf")
print(str1)
// true
  • 转Int
let str5 = "123"
let str1: Int = Int(str5) ?? 0
print(str1)
// 123

let str5 = "123a"
let str1: Int = Int(str5) ?? 0
print(str1)
// 0

你可能感兴趣的:(Swift5.0 字符串(String)详解)