TDTSwiftyString 0.5.0

TDTSwiftyString 0.5.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2018
SPMSupports SPM

Maintained by Daisuke Todate.



  • By
  • Todate

TDTSwiftyString

CI Status Version License Platform Swift-4.1

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

TDTSwiftyString is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "TDTSwiftyString"

Usage

import TDTSwiftyString

Methods

length

"".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"

subscript

// 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"

=~ (RegularExpression operator)

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

License

TDTSwiftyString is available under the MIT license. See the LICENSE file for more info.