iOS-Swift:判断字符串数组中是否包含某个字符串

swift5.2

Case1

  1. [String] 转换成 String
let class3_2: [String] = ["LiMing", "LiHua", "XiaoWang", "UziIsStillAlive"]
let studentsName = class3_2.joined(separator: "")
  1. 判断是否包含所求字符串
if studentsName.range(of: "Uzi") != nil {
	//如果包含字符串
	print("Uzi! He is still alive!")
} 

注意:
这种情况是判断数组中的某个长 String 是否又包含了某个短 String

Case2

let class3_2 = ["LiMing", "LiHua", "XiaoWang", "Uzi"]
let studentName = "Uzi"

if class3_2.contains(where: { $0.caseInsensitiveCompare(studentName) == .orderedSame }) {
    print("Uzi! He is still alive!")
}

这种情况是判断数组中是否包含所求的 String

你可能感兴趣的:(iOS-Swift:判断字符串数组中是否包含某个字符串)