A zero-dependency pure Swift library which adds useful functionality to the String Protocol. By extending String Protocol, all of these methods become available on Strings as well as Substrings and other SubSequences so you can chain them together. Most of the functionality in this library returns a subsequence rather than a new string, which saves on memory usage.
This package requires Swift 5 and Xcode 11 (package is untested on earlier versions of Swift and Xcode)
Add this package to your package.swift
.package(url: "https://github.com/robertmsale/StringFix" from: "1.0.0")
import StringFix
Subscripting with Integers is now allowed
"Hello World!"[2...6] // "llo W"
"Hello World!"[6...] // "World!"
"Hello World!"[...4] // "Hello"
"Hello World!"[0..<11] // "Hello World"
Subscripts can be chained together
"https://just1guy.org"[8...][..<8] // "just1guy"
"##foo##".between("##") // "foo"
"<a>some link</a>".between("<a>", "</a>") // "some link"
"## ##foo## ##".between("##") // " ##foo## "
"##foo##".outerBetween("##") // "##foo##"
"<a>some link</a>".outerBetween("<a>", "</a>") // "<a>some link</a>"
"## ##foo## ##".outerBetween("##") // "## ##foo## ##"
"id number".camelize() // "idNumber"
"HelloWorld".camelize() // "helloWorld"
"text_size".camelize() // "textSize"
"first-name".camelize() // "firstName"
" So much \t\n space! " // "So much space!"
"/etc/config".ensureLeft("/") // "/etc/config"
"etc/config".ensureLeft("/") // "/etc/config"
"https://just1guy.org/".ensureRight("/") // "https://just1guy.org/"
"https://just1guy.org".ensureRight("/") // "https://just1guy.org/"
"foobar".isAlpha() // true
"f00b4r".isAlpha() // false
"FooBar".isAlpha() // true
"FooBar5".isAlphaNumeric() // true
"123abc".isAlphaNumeric() // true
"-32".isAlphaNumeric() // false
"123".isNumeric() // true
"f00b4r".isNumeric() // false
" ".isEmpty() // true
"\n\t".isEmpty() // true
" foo ".isEmpty() // false
" \n\t Hello World!".trimLeft() // "Hello World!"
"Hello World! \n\t ".trimRight() // "Hello World!"
" \t Hello World! \t ".trim() // "Hello World!"
"So... many, do:t(s)!".stripPunctuation() // "So many dots"
"Pad me please".padLeft(4) // " Pad me please"
"Cool words".padLeft(2, "~") // "~~Cool words"
"Foobar".padRight(4) // "Foobar "
"Hello".padRight(3, "!") // "Hello!!!"
"Swift is awesome".pad(3) // " Swift is awesome "
"Hello".padRight(3, "!") // "Hello!!!"
"Foo".times(5) // "FooFooFooFooFoo"