avatar
童琦杰
Oct 21, 2022Technology

Swift - 字符串扩展

字符串查询

输出参数为Int类型,而非String.Index类型,更易理解

swift
public extension String {

    func indexOf(value: Character) -> Int? {
        if let index = self.firstIndex(of: value) {
            return self.distance(from: self.startIndex, to: index)
        } else {
            return nil
        }
    }

    func lastIndexOf(value: Character) -> Int? {
        if let index = self.lastIndex(of: value) {
            return self.distance(from: self.startIndex, to: index)
        } else {
            return nil
        }
    }
}

子字符串提取(substring)

输入参数采用Int类型,而非String.Index类型,编码更加简洁。同时处理了索引越界问题。

swift
public extension String {

    func substring(startIndex: Int) -> String {
        if let index = self.index(self.startIndex, offsetBy: startIndex, limitedBy: self.endIndex) {
            return String(self[index...])
        } else {
            return ""
        }
    }

    func substring(startIndex: Int, length: Int) -> String {
        if let start = self.index(self.startIndex, offsetBy: startIndex, limitedBy: self.endIndex) {
            if let end = self.index(self.startIndex, offsetBy: startIndex + length, limitedBy: self.endIndex) {
                return String(self[start..<end])
            } else {
                return String(self[start...])
            }
        } else {
            return ""
        }
    }
}

trim方法

支持更多形式的trim方式,包括空白字符trim、前缀后缀trim、指定字符trim等方式。

swift
public extension String {

    func trim() -> String {
        return self.trimmingCharacters(in: .whitespacesAndNewlines)
    }

    func trimStart() -> String {
        guard let index = firstIndex(where: { !CharacterSet(charactersIn: String($0)).isSubset(of: .whitespacesAndNewlines) }) else {
            return self
        }
        return String(self[index...])
    }

    func trimEnd() -> String {
        guard let index = lastIndex(where: { !CharacterSet(charactersIn: String($0)).isSubset(of: .whitespacesAndNewlines) }) else {
            return self
        }
        return String(self[...index])
    }

    func trim(trimChar: Character) -> String {
        let start: Index
        if let index = self.firstIndex(where: { x in x != trimChar }) {
            start = index
        } else {
            start = self.startIndex
        }
        let end: Index
        if let index = self.lastIndex(where: { x in x != trimChar }) {
            end = index
        } else {
            end = self.endIndex
        }
        return String(self[start...end])
    }

    func trimStart(trimChar: Character) -> String {
        let start: Index
        if let index = self.firstIndex(where: { x in x != trimChar }) {
            start = index
        } else {
            start = self.startIndex
        }
        return String(self[start...])
    }

    func trimEnd(trimChar: Character) -> String {
        let end: Index
        if let index = self.lastIndex(where: { x in x != trimChar }) {
            end = index
        } else {
            end = self.endIndex
        }
        return String(self[...end])
    }

    func trim(_ trimChars: Character...) -> String {
        let start: Index
        if let index = self.firstIndex(where: { x in !trimChars.contains(x) }) {
            start = index
        } else {
            start = self.startIndex
        }
        let end: Index
        if let index = self.lastIndex(where: { x in !trimChars.contains(x) }) {
            end = index
        } else {
            end = self.endIndex
        }
        return String(self[start...end])
    }

    func trimStart(_ trimChars: Character...) -> String {
        let start: Index
        if let index = self.firstIndex(where: { x in !trimChars.contains(x) }) {
            start = index
        } else {
            start = self.startIndex
        }
        return String(self[start...])
    }

    func trimEnd(_ trimChars: Character...) -> String {
        let end: Index
        if let index = self.lastIndex(where: { x in !trimChars.contains(x) }) {
            end = index
        } else {
            end = self.endIndex
        }
        return String(self[...end])
    }
}

文本替换(replace)

swift
public extension String {

    func replace(oldValue: String, newValue: String) -> String {
        return self.replace(oldValue: oldValue, newValue: newValue, ignoreCase: false)
    }

    func replace(oldValue: String, newValue: String, ignoreCase: Bool) -> String {
        if ignoreCase {
            return self.replacingOccurrences(of: oldValue, with: newValue, options: .caseInsensitive, range: nil)
        } else {
            return self.replacingOccurrences(of: oldValue, with: newValue)
        }
    }
}

前缀、后缀检查

swift
public extension String {

    func startsWith(value: String) -> Bool {
        return self.startsWith(value: value, ignoreCase: false)
    }

    func startsWith(value: String, ignoreCase: Bool) -> Bool {
        if ignoreCase {
            return self.lowercased().hasPrefix(value.lowercased())
        } else {
            return self.hasPrefix(value)
        }
    }

    func endsWith(value: String) -> Bool {
        self.endsWith(value: value, ignoreCase: false)
    }

    func endsWith(value: String, ignoreCase: Bool) -> Bool {
        if ignoreCase {
            return self.lowercased().hasSuffix(value.lowercased())
        } else {
            return self.hasSuffix(value)
        }
    }
}
© 2015-2022 tongqijie.com 版权所有沪ICP备17000682号