let s = " / 2 3 4 ? / "
// 替换
print("空格替换成-:", s.replacingOccurrences(of: " ", with: "-"))
// 过滤
print("空格过滤掉:", s.replacingOccurrences(of: " ", with: ""))
// 去首尾空格
print("去掉空格:", s.trimmingCharacters(in: .whitespaces))
// 分割
print("分割:", s.components(separatedBy: "/"))
// 拼接
let a = ["1", "2", "3"]
print("拼接:", a.joined(separator: "-"))
字符串截取:
let timeString = "2016.12.12"
let timeString = "2016.12.12"
let index = timeString.index(timeString.endIndex, offsetBy: -2)
let suffix = timeString.substring(from: index)
let index1 = timeString.index(timeString.startIndex, offsetBy: 2)
let prefix = timeString.substring(to: index1)
let startRange = timeString.index(timeString.startIndex, offsetBy: 5)
let endRange = timeString.index(timeString.startIndex, offsetBy: 7)
let middle = timeString.substring(with: Range(startRange..<endRange))
Range使用:
let str = “反正我是帅的不要不要!!!你呢?"
let startIndex = str.startIndex.advancedBy(3) //参数必须大于等于0
//以前用法 let startIndex = advance(self.startIndex, r.startIndex)
let endIndex = str.endIndex.advancedBy(-3) //参数必须小于0
let range = Range<String.Index>(start: startIndex, end: endIndex)
swift3.0改变为:
let range = Range(startIndex..<endIndex)
print(str.startIndex)//起始位置
print(str.endIndex)//结束位置
print(str.substringWithRange(range))//截取字符串
print(str[startIndex])//开始位置的字符
print(str[endIndex])//结束位置的字符
//建议转成把String转成NSString....
let string = sender.text!
var count = 0
for c in string.characters {
print(c)
print(c.debugDescription)
if c.debugDescription.validLeterAndFigure {
count += 1
} else {
count += 2
}
}
Range使用举例:
左到右截取
startIndex to offsetBy正数
右到左截取
endindex from offsetBy负数
func ga_dateToDay() -> String {
let index = self.index(self.endIndex, offsetBy: -2)
let suffix = self.substring(from: index)
// substring(to: index)
let startRange = self.index(self.startIndex, offsetBy: 5)
let endRange = self.index(self.startIndex, offsetBy: 7)
let middle = self.substring(with: Range(startRange..<endRange))
// 月
let middleInt = Int(middle)
// 日
let suffixInt = Int(suffix)
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.month, .day], from: Date())
// 月
let month = components.month
// 日
let day = components.day
if month == middleInt {
if day == suffixInt {
return "今天"
}
if day! > suffixInt! {
if day! - suffixInt! == 1 {
return "昨天"
}
return self
}
} else {
if month! - middleInt! == 1 {
if day == 1 {
return "昨天"
}
}
}
return self
}