TDTSwiftyString 0.5.0

TDTSwiftyString 0.5.0

测试已测试
语言支持语言 SwiftSwift
许可证 MIT
发布最后发布2018年6月
SPM支持使用 SPM

Daisuke Todate 维护。



  • 作者
  • Todate

TDTSwiftyString

CI Status Version License Platform Swift-4.1

示例

要运行示例项目,请先克隆仓库,然后在 Example 目录中运行 pod install

安装

TDTSwiftyString 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 即可

pod "TDTSwiftyString"

使用方法

import TDTSwiftyString

方法

长度

"".length // 0
"0123456789".length // 10

indexOf(target)

"0123456789".indexOf("nothing") // -1
"0123456789".indexOf("01") // 0
"0123456789".indexOf("3456") // 3

indexOf(target, startIndex)

"constant".indexOf("n", startIndex: 3) // 6

lastIndexOf(target)

"changing".lastIndexOf("g") // 7
"meeting".lastIndexOf("e") // 2

substring(from)

"0123456789".substring(from: 0) // "0123456789"
"0123456789".substring(from: 3) // "3456789"
"0123456789".substring(from: 9) // "9"
"0123456789".substring(from: -1) // "9"
"0123456789".substring(from: -3) // "789"

substring(to)

"0123456789".substring(to: 0) // "0"
"0123456789".substring(to: 3) // "0123"
"0123456789".substring(to: 9) // "0123456789"
"0123456789".substring(to: -1) // "0123456789"
"0123456789".substring(to: -3) // "01234567"

substring(from, to)

"0123456789".substring(from: 1, to: 1) // "1"
"0123456789".substring(from: 1, to: 5) // "12345"

substring(from, length)

"0123456789".substring(from: 1, length: 0) // ""
"0123456789".substring(from: 1, length: 1) // "1"
"0123456789".substring(from: 1, length: 7) // "1234567"

下标

// index
"0123456789"[0] // "0"
"0123456789"[5] // "5"
"0123456789"[9] // "9"
"0123456789"[-1] // "9"
"0123456789"[-2] // "8"
"0123456789"[-9] // "1"
"0123456789"[-10] // "0"

// range
"0123456789"[0..<0] // ""
"0123456789"[5..<6] // "5"
"0123456789"[0..<1] // "0"
"0123456789"[8..<9] // "8"
"0123456789"[1..<5] // "1234"
"0123456789"[0..<9] // "012345678"

// closed range
"0123456789"[0...0] // "0"
"0123456789"[5...6] // "56"
"0123456789"[0...1] // "01"
"0123456789"[8...9] // "89"
"0123456789"[1...5] // "12345"
"0123456789"[0...9] // "0123456789"

=~ (正则表达式运算符)

let emailRegex = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$"

emailRegex =~ "[email protected]" // 0
emailRegex =~ "email-test.com" // nil

"test" =~ "[email protected]" // 6

isMatch(pattern, options)

let emailRegex = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$"

"[email protected]".isMatch(emailRegex, options: .caseInsensitive) // true
"email-test.com".isMatch(emailRegex, options: .caseInsensitive) // false

getMatches(pattern, options)

let emailRegex = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$"
let testText = "[email protected], [email protected], [email protected]"

let matches = testText.getMatches(emailRegex, options: .caseInsensitive)

for (idx, match) in matches.enumerated() {
    let range = match.range

    if range.location != NSNotFound {
        let matchString = testText.substring(from: range.location, length: range.length)

        if !matchString.isEmpty {
            print(matchString)
        }
    }
}
let testText = "CamelCaseText"
let words = ["Camel", "Case", "Text"]
let pattern = "[A-Z][a-z]+"

let matches: [String] = testText.getMatches(pattern, options: [])   // ["Camel", "Case", "Text"]

nsRange(target)

var testText = "0123456789"
var target = "78"

var range = testText.nsRange(of: target)
print(range.location) // 7
print(range.length) // 2

许可证

TDTSwiftyString 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。