OC和Swift中字符串的区别
- 在OC中字符串类型时NSString,在Swift中字符串类型是String
- OC中字符串@"",Swift中字符串""
//1、字符串声明
var ch999Str1 : String = " " //声明一个空字符串
var ch999Str2 = "hans" // 声明时候可以忽略类型,未定义变量类型,在swift中会根据值推测变量的类型为String
var ch999Str3 = String() // initializer syntax,空初始化
//可以通过检查布尔量 isEmpty属性来确认一个 String值是否为空:
if ch999Str3.isEmpty {
print("我是空的看到了不?")
}
var ch999Str4 : String?
if ch999Str4 == nil {
ch999Str4 = "hehe"
}
//2、字符串拼接
ch999Str1 = "九"
ch999Str2 = "机"
ch999Str3 = ch999Str1 + ch999Str2//利用+直接将字符串拼接在一起,比较直接
let array : [String] = ["c","h", "999"]
let arrayStr : String = array.joined(separator: "/")//将数组中的元素拼接成字符串:c/h/999
//3、字符串的截取
//(1)index
ch999Str1 = "六剑客与小白"
var ch999str44 = "六剑客与小白六剑客与小白"
//let index = ch999Str1.startIndex.advancedBy(5) //弃用了
let index1 = ch999Str1.index(ch999Str1.endIndex, offsetBy: -2)
let index2 = ch999Str1.index(ch999Str1.startIndex, offsetBy: 3)
//substringFromIndex:是包括当前下标的值开始截取,到字符串最后停止
ch999Str2 = ch999str44.substring(from: index1)
//substringToIndex:是截取到当前下标为止
ch999Str3 = ch999str44.substring(to: index1)
//limitedBy 给定限制范围,确保不会越界
if let index3 = ch999str44.index(ch999str44.startIndex, offsetBy:15, limitedBy: ch999str44.endIndex){
print("index3str = \(ch999str44[index3])")//y
}else{
print("越界了")//y
}
print("ch999Str2 = \(ch999Str2) \nch999Str3 =\(ch999Str3)\nindex = \(index2)" )
//(2)range
let rangeee = 1...4
let afterRangeee = ch999str44.index(after: ch999str44.startIndex )..<ch999str44.index(ch999str44.endIndex, offsetBy: -2)
print("ch999Str2 = \(ch999str44[afterRangeee]) \nch999Str3 =\(ch999str44[afterRangeee])\nrange = \(afterRangeee)" )
//4、字符串的分割
//components(separatedBy:)方法是将字符串根据指定的字符编码参数进行分割,并将分别的内容转换为一个数组。其语法形式如下:
//func components(separatedBy separator: CharacterSet) -> [String]
//其中,separator用来指定分割的字符集。
var ch999Str5 = "六,剑,客,与,小,白"
var array2 = ch999Str5.components(separatedBy: ",")
print("分割后:\(array2)\n\(ch999Str5.components(separatedBy: ","))")
//5、distance 查找字符的位置
ch999str44 = "六剑客与小白六剑客与小白"
let needle: Character = "白"
if let idx = ch999str44.characters.index(of:needle) {
let pos = ch999str44.characters.distance(from:ch999str44.startIndex, to: idx)
print("Found\(needle) at position\(pos)")//Found . at position 5
}
else {
print("Not found")
}
//6丶使用字符串替换指定范围的子字符串
//(1)全局替换
ch999str44 = ch999str44.replacingOccurrences(of: "与", with: "和")
print(ch999str44)
//(2)局部替换
ch999str44.replaceSubrange(ch999str44.startIndex..<ch999str44.index(ch999str44.startIndex, offsetBy: 6), with:"ch999")
print("\(ch999str44)") // ch999六剑客与小白
//根据NSCharacterSet查找
let searchRange = ch999str44.startIndex ..< ch999str44.characters.index(ch999str44.startIndex, offsetBy: 7)
let cSet = CharacterSet.uppercaseLetters
var range = ch999str44.rangeOfCharacter(from: cSet)
print(range!)
//根据字符串查找
range = ch999str44.range(of: "小白")
print(range!)
range = ch999str44.range(of: "小白", options: NSString.CompareOptions.caseInsensitive, range: searchRange, locale: nil)
if let tempRange = range {
print("tempRange:\(tempRange)")
}
// var c:CharacterSet = NSCharacterSet(charactersIn:"小白") as CharacterSet
//
// var ranges = ch999str44.rangeOfCharacter(from: c)
//
//
//
//7丶判断包不包含
//contains(_:)方法用来判断字符串A是否包含aString字符串,其语法形式如下:
//func contains(_ str: String) -> Bool
//其中,str用来指定一个字符串。
if(ch999str44.contains("小白")){
print("ch999str44字符串中包含小白字符串")
}else{
print("ch999str44字符串中不包含小白字符串")
}