Skip to content
Swift Library for extended String Functionality
Swift
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources/StringFix
Tests
.gitignore
LICENSE.md
Package.swift
README.md

README.md

StringFix

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.

Installation

Prerequisites

This package requires Swift 5 and Xcode 11 (package is untested on earlier versions of Swift and Xcode)

Add To Project

Add this package to your package.swift

.package(url: "https://github.com/robertmsale/StringFix" from: "1.0.0")

Usage

import StringFix

Subscripts

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"

Methods

between (_ begin: String, _ end: String? = nil) -> SubSequence

"##foo##".between("##")                      // "foo"
"<a>some link</a>".between("<a>", "</a>")    // "some link"
"## ##foo## ##".between("##")                // " ##foo## "

outerBetween (_ begin: String, _ end: String? = nil) -> SubSequence

"##foo##".outerBetween("##")                      // "##foo##"
"<a>some link</a>".outerBetween("<a>", "</a>")    // "<a>some link</a>"
"## ##foo## ##".outerBetween("##")                // "## ##foo## ##"

camelize () -> String

"id number".camelize()      // "idNumber"
"HelloWorld".camelize()     // "helloWorld"
"text_size".camelize()      // "textSize"
"first-name".camelize()     // "firstName"

collapseWhitespace() -> String

"  So  much \t\n space!  "  // "So much space!"

ensureLeft() -> String

"/etc/config".ensureLeft("/")   // "/etc/config"
"etc/config".ensureLeft("/")    // "/etc/config"

ensureRight() -> String

"https://just1guy.org/".ensureRight("/")    // "https://just1guy.org/"
"https://just1guy.org".ensureRight("/")     // "https://just1guy.org/"

isAlpha() -> Bool

"foobar".isAlpha()      // true
"f00b4r".isAlpha()      // false
"FooBar".isAlpha()      // true

isAlphaNumeric() -> Bool

"FooBar5".isAlphaNumeric()      // true
"123abc".isAlphaNumeric()       // true
"-32".isAlphaNumeric()          // false

isNumeric() -> Bool

"123".isNumeric()       // true
"f00b4r".isNumeric()    // false

isEmpty() -> Bool

"   ".isEmpty()         // true
"\n\t".isEmpty()        // true
"  foo  ".isEmpty()     // false

trimLeft() -> SubSequence

"   \n\t Hello World!".trimLeft()       // "Hello World!"

trimRight() -> SubSequence

"Hello World!   \n\t  ".trimLeft()      // "Hello World!"

trim() -> SubSequence

" \t Hello World! \t  ".trim()          // "Hello World!"

stripPunctuation() -> String

"So... many, do:t(s)!".stripPunctuation()   // "So many dots"

padLeft(_ count: Int, _ with: String = " ") -> String

"Pad me please".padLeft(4)              // "    Pad me please"
"Cool words".padLeft(2, "~")            // "~~Cool words"

padRight(_ count: Int, _ with: String = " ") -> String

"Foobar".padRight(4)              // "Foobar    "
"Hello".padRight(3, "!")            // "Hello!!!"

pad(_ count: Int, _ with: String = " ") -> String

"Swift is awesome".pad(3)              // "   Swift is awesome   "
"Hello".padRight(3, "!")            // "Hello!!!"

times(_ count: Int) -> String

"Foo".times(5)          // "FooFooFooFooFoo"
You can’t perform that action at this time.