let 二维码内容 = "aoxiaomi"
let 二维码网址 = "www.baidu.com/aoxiaomi"
let btn1:UIButton()
let btn2:UIButton()
btn1.addTarget(self,"btn1Action",true)
btn2.addTarget(self,"btn2Action",true)
func btn1Action(){
页面跳转到www.baidu.com
textField.text = 二维码内容
}
func btn2Action(){
let url = 拆解 二维码网址 / 之前的字符串 (www.baidu.com)
let 内容 = 拆解 二维码网址 / 之后的字符串 (aoxiaomi)
页面跳转到url
textField.text = 内容
}
判断字符串你是否为空
let words = "aoxiaomi.com"
if !words.isEmpty {
print("is not empty")//is not empty
}
是否包含子字符串
if words.rangeOfString("mi") != nil {
print("true")
}
if (words as NSString).containsString("mi"){
print("true")
}
字符串截取
//转换成NSString
let mi = (words as NSString).substringWithRange(NSMakeRange(6,2)) //mi
//不转换
let left = words.startIndex.advancedBy(6)
let right = words.startIndex.advancedBy(8)
let range = Range<String.Index>(start: left, end: right)
let mi = words.substringWithRange(range) //mi
文字 -> %BD%A0%C2
var urlWords = String()
for c in words.utf8 { ↓十进制转十六进制↓
urlWords += "%\(String(format: "%1X", c))"
}
替换
let w = words.stringByReplacingOccurrencesOfString("ao", withString: "AO")//AOxiaomi