A set of useful string extensions.
Stringify is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Stringify', '~> 1.0'
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding Stringify as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/NovichenkoAnton/Stringify.git", .upToNextMajor(from: "1.0.0"))
]
CountableRange
, ClosedRange
, PartialRangeFrom
, PartialRangeThrough
, PartialRangeUpTo
.let cardNumber = "1234567890123456"
let masked = cardNumber.maskSubstring(in: 6...13, with: "*")
print(masked!) //"123456********56"
String
to Double
. If String
is not compatible with Double
the function will return 0.00.let amount = "100,12"
print(amount.toDouble()) //100.12
let anotherAmount = "1 200,10"
print(anotherAmount.toDouble()) //1200.1
let sum = "1234"
let formattedSum = sum.st.applyFormat(.sum)
print(formattedSum) //"1 234,00"
Supported formats
enum Format {
case sum(minFractionDigits: Int = 2, maxFractionDigits: Int = 2)
case creditCard
case iban
case custom(formatter: NumberFormatter)
}
Validate a number of credit card by Luhn algorithm.
Validate the string with specific pattern
"https://www.google.com".validate(with: .website) //true
let dateTime = "2019-11-22 13:33"
let resultTime = dateTime.st.convertDate(from: "yyyy-MM-dd HH:mm", to: "h:mm") //"1:33"
String
that corresponds to URL
type. Works for URLs with cyrillic domain names.let stringURL = "https://test.com?foo=1&bar=abc"
let queryItems = stringURL.queryItems()! //["foo": "1", "bar": "abc"]
+
let part1 = "123"
let part2 = "456"
myLabel.attributedText = part1.attributed + part2.attributed
let string = "Some text"
label.attributedText = string.attributed.applyAttributes([
.color(color: .red),
.font(font: .systemFont(ofSize: 32, weight: .bold)),
.crossed(width: 1, color: .black),
.underline(style: .single, color: .blue)
])
let sum = "1000,22"
label.attributedText = sum.attributed.applyStyle(.sum(integerAttrs: [
.color(color: UIColor.red),
.font(font: UIFont.systemFont(ofSize: 32, weight: .bold)),
.underline(style: .double, color: .black)
], fractionAttrs: [
.color(color: UIColor.green),
.font(font: UIFont.systemFont(ofSize: 24, weight: .medium)),
], currencyMark: "$"))
link |
Stars: 6 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics