在Swift中进行字符串的截取的时候, 不像使用NSString那么方便, 这是因为String的截取方法substring(from: <#T##String.Index#>)所需要的参数不是Int类型, 这在使用的时候就很不方便, 这里我对String写了一个Extension, 用法十分简单:
Swift 3.0
extension String {
// 字符串的第一个字符
var first: String {
return self.substringTo(0)
}
// 字符串的最后一个字符
var last: String {
return self.substringFrom(self.characters.count - 1)
}
// 字符串开始到第index
func substringTo(_ index: Int) -> String {
guard index < self.characters.count else {
assertionFailure("index beyound the length of the string")
return ""
}
let theIndex = self.index(self.startIndex, offsetBy: index + 1)
return self.substring(to: theIndex)
}
// 从第index个开始到结尾的字符
func substringFrom(_ index: Int) -> String {
guard index < self.characters.count else {
assertionFailure("index beyound the length of the string")
return ""
}
guard index >= 0 else {
assertionFailure("index can't be lower than 0")
return ""
}
let theIndex = self.index(self.endIndex, offsetBy: index - self.characters.count)
return self.substring(from: theIndex)
}
// 某个闭区间内的字符
func substringInRange(_ range: CountableClosedRange<Int>) -> String {
guard range.lowerBound >= 0 else {
assertionFailure("lowerBound of the Range can't be lower than 0")
return ""
}
guard range.upperBound < self.characters.count else {
assertionFailure("upperBound of the Range beyound the length of the string")
return ""
}
let start = self.index(self.startIndex, offsetBy: range.lowerBound)
let end = self.index(self.startIndex, offsetBy: range.upperBound + 1)
let strRange = Range.init(uncheckedBounds: (lower: start, upper: end))
return self.substring(with: strRange)
}
mutating func hiddenTelphone() {
let start = self.index(self.startIndex, offsetBy: 3)
let end = self.index(self.endIndex, offsetBy: -4)
let range = Range.init(uncheckedBounds: (lower: start, upper: end))
self.replaceSubrange(range, with: "****")
// self.replaceSubrange(3...6, with: "****")
}
}
Swift 4.0+
//
// LQString.swift
// LQProjectorSwift
//
// Created by LiuQiqiang on 2018/6/30.
// Copyright © 2018年 LiuQiqiang. All rights reserved.
//
import UIKit
extension String {
/// 字符串的第一个字符
var firstString: String {
return self.substringTo(0)
}
/// 字符串的最后一个字符
var lastString: String {
return self.substringFrom(self.count - 1)
}
/// 字符串开始到第index
///
/// - Parameter index: 结束索引
/// - Returns: 子字符串
func substringTo(_ index: Int) -> String {
guard index < self.count else {
assertionFailure("index beyound the length of the string")
return ""
}
let theIndex = self.index(self.startIndex, offsetBy: index)
return String(self[startIndex...theIndex])
}
/// 从第index个开始到结尾的字符
///
/// - Parameter index: 开始索引
/// - Returns: 子字符串
func substringFrom(_ index: Int) -> String {
guard index < self.count else {
assertionFailure("index beyound the length of the string")
return ""
}
guard index >= 0 else {
assertionFailure("index can't be lower than 0")
return ""
}
let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
return String(self[theIndex..<endIndex])
}
/// 某个闭区间内的字符
///
/// - Parameter range: 闭区间,例如:1...6
/// - Returns: 子字符串
func substringInRange(_ range: CountableClosedRange<Int>) -> String {
guard range.lowerBound >= 0 else {
assertionFailure("lowerBound of the Range can't be lower than 0")
return ""
}
guard range.upperBound < self.count else {
assertionFailure("upperBound of the Range beyound the length of the string")
return ""
}
let start = self.index(self.startIndex, offsetBy: range.lowerBound)
let end = self.index(self.startIndex, offsetBy: range.upperBound + 1)
return String(self[start..<end])
}
/// 手机号字符串隐藏中间部分数字
mutating func hiddenTelphone() {
if self.count != 11 {
return ;
}
let start = self.index(self.startIndex, offsetBy: 3)
let end = self.index(self.endIndex, offsetBy: -4)
let range = Range.init(uncheckedBounds: (lower: start, upper: end))
self.replaceSubrange(range, with: "****")
// self.replaceSubrange(3...6, with: "****")
}
}
使用:
let str = "我爱写BUG,写BUG使我快乐"
print("前部分字符: \(str.substringTo(3))")
print("后部分字符: \(str.substringFrom(11))")
print("区域: \(str.substringInRange(10...13))")
print("第一个字符串: \(str.first)")
print("最好一个字符: \(str.last)")
输出:
前部分字符: 我爱写B
后部分字符: 使我快乐
区域: G使我快
第一个字符串: 我
最好一个字符: 乐
是不是很简单呢?