Swift截取字符串(String.Index)
2018-01-23 本文已影响0人
向日葵的夏天_summer
extension String {
func toInt() -> Int? {
return Int(self)
}
func toFloat() -> Float? {
return Float(self)
}
func toDouble() -> Double? {
return Double(self)
}
//MARK:- 去除字符串两端的空白字符
func trim() -> String {
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
func subString(to: Int) -> String {
var to = to
if to > self.count {
to = self.count
}
return String(self.prefix(to))
}
func subString(from: Int) -> String {
if from >= self.count {
return ""
}
let startIndex = self.index(self.startIndex, offsetBy: from)
let endIndex = self.endIndex
return String(self[startIndex..<endIndex])
}
func subString(start: Int, end: Int) -> String {
if start < end {
let startIndex = self.index(self.startIndex, offsetBy: start)
let endIndex = self.index(self.startIndex, offsetBy: end)
return String(self[startIndex..<endIndex])
}
return ""
}
}