字符串插入:str.insert(" ", at: str.index(str.startIndex, offsetBy:3))
字符串替换:str.replacingOccurrences(of: " ", with: "")
截取字符串:
前3个:str.prefix(3)
后3个:str.substring(from: 3)
打印字符串:NSLog("\(sub1) \(sub2) \(sub3) \(sss)")
functextField(_textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) ->Bool{
ifstring.isNonEmpty(),varstr = textField.text{
str += string
str = str.replacingOccurrences(of:" ", with:"")
ifstr.count>3{
str.insert(" ", at: str.index(str.startIndex, offsetBy:3))
}
ifstr.count>8{
str.insert(" ", at: str.index(str.startIndex, offsetBy:8))
}
textField.text= str
returnfalse
}
return true
}
网友评论