在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
string常用的一些操作方式
//字符串 //1.初始化字符串 //1.1通过字面量赋值的方式初始化字符串 let tempStrig = "this is temp string" let tempStrig = "this is TEMP string" //字母大小写 print(tempStrig.uppercased()) print(tempStrig.lowercased())
//字符串截取 let swiftString = "好喜欢!!!" //将swiftString 转换为OC 的字符串使用,根据范围来截取字符串 let subString1 = (swiftString as NSString).substring(with: NSMakeRange(1, 3)) print(subString1) //截取下标2 之前的字符 let subString2 = (swiftString as NSString).substring(to: 2) print(subString2) //截取下标2 之后(含有2) let subString3 = (swiftString as NSString).substring(from: 2) print(subString3) var insertStr = "明天星期天" //插入 //字符串最后 insertStr.insert("a", at: insertStr.endIndex) print(insertStr) //字符串最前 insertStr.insert("a", at: insertStr.startIndex) print(insertStr) //在指定位置插入 insertStr.insert("g", at: insertStr.index(before: insertStr.index(insertStr.startIndex, offsetBy: 3))) print(insertStr) //移除指定下标字符 insertStr.remove(at: insertStr.index(before: insertStr.index(insertStr.startIndex, offsetBy: 1))) print(insertStr) //系统提供的方法挺多的,大家有兴趣可以多看看
//字符串比较 //比较字符是否相同 let s1 = "好的" let s2 = "好的" if s1 == s2 { print("s1 == s2") }else{ print("s1 != s2") } let s3:NSString = "ok" let s4:NSString = "ok!" if s3.isEqual(to: s4 as String) { print("s3 == s4") }else{ print("s3 != s4") } //比较字符串的前缀,后缀 let array = ["do.docx","good.docx","name.docx","data.json","good.json"] for d in array { if d.hasPrefix("good"){ print("前缀为good:\(d)") } } for d in array { if d.hasSuffix(".json"){ print("后缀为.json:\(d)") } }
|
请发表评论