Swiftpack.co - Package - ArtSabintsev/Guitar

Guitar 🎸

A Cross-Platform String and Regular Expression Library written in Swift.

Build Status Platform

Swift Support CocoaPods SwiftPM Compatible


This library seeks to add common string manipulation functions, including common regular expression capabilities, that are needed in both mobile and server-side development, but are missing in Swift's Standard Library.

The full documentation can be found at http://www.sabintsev.com/Guitar/.

Guitar is also part of Swift's Source Compatibility Suite.


  • ☑ Boolean Functions (isAlpha, isNumeric, isUppercase, etc.)
  • ☑ Case Functions (camelCased, pascalCased, kebabCased, etc.)
  • ☑ Latinization Functions (.latinized(), .withoutAccents())
  • ☑ Padding Functions (padLeft, padRight, pad)
  • ☑ Regular Expressions (with Common Patterns Built-in)


A small set of functions offered by Guitar were removed in v0.3.0 of the library as Apple added many of these features to the Swift 4 language (aka, Sherlocking). The functions that were removed were: first, last, length, prefixed, suffixed, trimLeft, trimRight, truncated. The reversed function was renamed to reversedString as it acts slightly differently than Swift's built-in reversed function.

Installation Instructions

Swift Version Branch Name Will Continue to Receive Updates?
5.1+ master Yes
5.0 swift5.0 No
4.2 swift4.2 No
4.1 swift4.1 No
3.2 swift3.2 No
3.1 swift3.1 No


pod 'Guitar' # Swift 5.1+
pod 'Guitar', :git => 'https://github.com/ArtSabintsev/Guitar.git', :branch => 'swift5.0' # Swift 5.0
pod 'Guitar', :git => 'https://github.com/ArtSabintsev/Guitar.git', :branch => 'swift4.2' # Swift 4.2
pod 'Guitar', :git => 'https://github.com/ArtSabintsev/Guitar.git', :branch => 'swift4.1' # Swift 4.1
pod 'Guitar', :git => 'https://github.com/ArtSabintsev/Guitar.git', :branch => 'swift3.2' # Swift 3.2
pod 'Guitar', :git => 'https://github.com/ArtSabintsev/Guitar.git', :branch => 'swift3.1' # Swift 3.1

Swift Package Manager

.Package(url: "https://github.com/ArtSabintsev/Guitar.git", majorVersion: 1)

Usage Examples

Regular Expression

  • Guitar makes it easier to use Regular Expressions to test and evaluate String objects.
  • Guitar.Chord enumerates common regular expressions and they are located in GuitarChord.swift.
  • GuitarCommon.swift contains a list of convenience methods for common regular expression evaluations and tests, such as checking the string to determine if it's a valid email address.


Guitar(pattern: String) // A custom regular expression with which to initialize Guitar.

Guitar(chord: Guitar.Chord) // A common regular expression with which to initialize Guitar.


evaluateForRanges(::) returns an array of ranges, [Range<String.Index>], that match a specific regular expression.

Guitar(chord: .firstCharacter).evaluateForRanges(from: "Hello world") // [Range(Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1)..<Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 1), _countUTF16: 1)), Range(Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 6), _countUTF16: 1)..<Swift.String.CharacterView.Index(_base: Swift.String.UnicodeScalarView.Index(_position: 7), _countUTF16: 1))]

evaluateForStrings(::) returns an array of strings, [String], that match a specific regular expression.

Guitar(chord: .firstCharacter).evaluateForStrings(from: "Hello world") // ["H", "w"]

test(::) evaluates a string with a specific regular expression. true is returned if matches are found in the string. Otherwise, false is returned.

Guitar(chord: .email).test(string: "hello@world.com") // `true`

String Extension

This library also adds dozens of methods via String extensions that are missing in the Swift Standard Library or not easily accessible on the String class. Check GuitarBoolean.swift and GuitarPadding.swift to see these examples.

More specific examples can be found in the tests folder.


Created and maintained by

Arthur Ariel Sabintsev


Stars: 635


Used By

Total: 0


Project Architecture Changes -

This is simply a a version bump so that less files are brought in when installing via SPM.

Fixed issue with evaluateForRanges -

#27: Thanks to @gezihuzi for noticing an issue with evaluateForRanges in Guitar.swift. The method now properly passes custom defined options.

Updated Manfiest File -

Fixed issue with Swift PM Manifest file.

Updated Manifest Files -

Updated Package.swift and Podspec files to resolve https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/399.

Adds Swift 5.1 / Xcode 11 / iOS 13 Support -

This release adds support for Swift 5.1, iOS 13, and Xcode 11. Full updates here: #26

Fixed Support for Swift Package Manager -

I fixed the Package.swift so that it is now compatible with Xcode 11 and the latest Swift Package Manifest file (// swift-tools-version:4.0).

Updated `#available` flags for certain GuitarCharacter methods -

Added #available(iOS 10.0, macOS 10.11, tvOS 10.0, watchOS 3.0, *) back to:

  • func latinized() -> String
  • func withoutAccents() -> String

Swift 5.0 Support -

This library has worked as-is for 2 years. Therefore, I am promoting this library to v1.0.0! 🍾

  • Updated for Swift 5
  • Drops iOS-9/tvOS-9 Support
  • #23: evaluateForRanges now supports UTF-16 encoded strings by default (thanks to @cjsliuj)

Swift 4.2 Support -

As of Guitar v0.5.0, , all future changes on master will be done in Xcode 10 using Swift 4.2.

For those using Swift v4.1.0, please check out the swift4.1 branch or version 0.4.1 - both point to the same commit hash.

Fixed Tests -

#21: Fixed Tests. Thanks to @felix91gr for the assistance.

Changes to accommodate Swift 4.1 -

Minor changes in a few places to make the library Swift 4.1 compatible.

Fixed Warnings and Updated Podspec -

  • Fixed .characters reference warning.
  • Updated Podspec file.
  • Removed .swift-version file, as it's been deprecated.

Support for Swift 4 / iOS 11 / Xcode 9 -

Siren v0.3.0 adds support for Swift 4 / iOS 11 / Xcode 9.

A small set of functions offered by Guitar were removed in this version as Apple added many of these features to the Swift 4 language. The functions that were removed were: first, last, length, prefixed, suffixed, trimLeft, trimRight, truncated. The reversed function was renamed to reversedString as it acts slightly differently than Swift's built-in reversed function.

Moving forward, only support for the Swift 4 codebase (on master) will be maintained.

Swift Version Branch Name Will Continue to Receive Updates?
4.0 master Yes
3.2 swift3.2 No
3.1 swift3.1 No

Swift PM Improvements -


  • Tests moved to Tests/ folder in root directory
  • Added support for Linux Tests.
  • Invoked #availability and #if os() checks where necessary.

Beta Release! -

As of v0.1.0, Guitar has transitioned from alpha to beta.

The existing API has stabilized and tests have been written for each public method and extension. Documentation has also been updated. To move from beta to a 1.0.0 release, more real-world testing is needed.

Please leave issues and PRs with any missing features or bugs!



Latinization function -

#16 Adds a latinization function (thanks to @EthanSchatzline)

    func testLatinized() {
        XCTAssertEqual("Hello! こんにちは! สวัสดี! مرحبا! 您好!".latinized(), "Hello! kon'nichiha! swasdi! mrhba! nin hao!")
        XCTAssertEqual("как прекрасен этот мир".latinized(), "kak prekrasen etot mir")
        XCTAssertEqual("你叫(做)乜野名呀?".latinized(), "ni jiao (zuo) mie ye ming ya?")
        XCTAssertEqual("어떻게 지내세요?".latinized(), "eotteohge jinaeseyo?")
        XCTAssertEqual("What a wonderful world! That's not cliché at all!".latinized(), "What a wonderful world! That's not cliche at all!")


Latinization Function -


  • @EthanSchatzline added the implementation and tests for withoutAccents(), which removes diacritics (e.g., accent marks) from latin characters. This is sometime referred to as latinization.

Guitar.Chord -

Chord is now an enum within the Guitar class, which is turning more into a Regex handling class.

Kebab Case and Slug Case -

Renamed slugCased function. Renamed it to kebabCased.

Removed kebabCased's old functionality as it was useless.

splitWordsByCase() Function -

In #12, I added a new splitWordsByCase() function that is able to create a string with whitespace-delimited words from any other case; camelCased, -kebab-cased-, PascalCase, snake_case, slug-case.

A few examples from the tests:

    func testCaseMorphing() {
        let string = "Hello World"

        XCTAssertEqual(string.camelCased().kebabCased(), "-hello-world-")
        XCTAssertEqual(string.camelCased().pascalCased(), "HelloWorld")
        XCTAssertEqual(string.camelCased().slugCased(), "hello-world")
        XCTAssertEqual(string.camelCased().snakeCased(), "hello_world")

        XCTAssertEqual(string.kebabCased().camelCased(), "helloWorld")
        XCTAssertEqual(string.kebabCased().pascalCased(), "HelloWorld")
        XCTAssertEqual(string.kebabCased().slugCased(), "hello-world")
        XCTAssertEqual(string.kebabCased().snakeCased(), "hello_world")

        XCTAssertEqual(string.pascalCased().camelCased(), "helloWorld")
        XCTAssertEqual(string.pascalCased().kebabCased(), "-hello-world-")
        XCTAssertEqual(string.pascalCased().slugCased(), "hello-world")
        XCTAssertEqual(string.pascalCased().snakeCased(), "hello_world")

        XCTAssertEqual(string.slugCased().camelCased(), "helloWorld")
        XCTAssertEqual(string.slugCased().kebabCased(), "-hello-world-")
        XCTAssertEqual(string.slugCased().pascalCased(), "HelloWorld")
        XCTAssertEqual(string.slugCased().snakeCased(), "hello_world")

        XCTAssertEqual(string.snakeCased().camelCased(), "helloWorld")
        XCTAssertEqual(string.snakeCased().kebabCased(), "-hello-world-")
        XCTAssertEqual(string.snakeCased().pascalCased(), "HelloWorld")
        XCTAssertEqual(string.snakeCased().slugCased(), "hello-world")

Massive Refactor -

  • Implemented Capitalized()
  • Fixed Decapitalized()
  • Enhanced slugCased(), kebabCased(), snakeCased() to allow for case-conversion from one to the other.
  • Fixed a lot of improper inline documentation.
  • Added a lot more tests
  • Split GuitarNSRangeExtension class from Guitar struct
  • Split GuitarChord struct from Guitar struct
  • Added more Chords to GuitarChord
  • Updated README
  • Updated Docs

Further Enhancements to Regex -

  • GuitarRegex has been renamed to Guitar.
  • Guitar(pattern:) initializer has been renamed to Guitar(chord:)
  • GuitarPattern is now accessed via Guitar.Chord
  • Removed prefix operator -
  • Added new sanitization function on Guitar
  • Fixed pascalCased().
  • Fixed some documentation.

See build details and artifacts at buddybuild: https://dashboard.buddybuild.com/apps/58b67d22d21c470100b0c394/build/58c23b0980c8d40100babafa

Fixed Carthage Support -

In the process of Fixing Swift PM support, I broke Carthage support. All dependency managers should now be able to reach and resolve Guitar properly.

Regex Support -

  • Separated String extensions into 5 separate files.
  • Added Regular Expression syntactic-sugar layer on top of NSRegularExpression.
  • Added stub for new capitalized() function that will augment Swift's capitalized computer variable.
  • Added more tests

API is still highly unstable, but things are progressing in the right direction.

See build details and artifacts at buddybuild: https://dashboard.buddybuild.com/apps/58b67d22d21c470100b0c394/build/58c18c4ea2fb9101008e402f

Swift PM Support -

Guitar now properly works with Swift PM.

Full Unit Test Coverage for Public Functions -

  • All public functions now have full Unit Test coverage except for decapitalized(), which has a broken implementation.
  • Renamed truncate() to truncated() and fixed implementation.
  • Added tests for all Trimming functions.

See build details and artifacts at buddybuild: https://dashboard.buddybuild.com/apps/58b67d22d21c470100b0c394/build/58b7caec95315a010041d9d3

Unit Tests & Implementation Fixes -

  • Added Unit Tests for Boolean, Case, Character, and Padding functions.
  • Added length function.
  • Fixed multiple functions (thanks to Unit Tests).

See build details and artifacts at buddybuild: https://dashboard.buddybuild.com/apps/58b67d22d21c470100b0c394/build/58b7c4722cb92b010066bcab

New Case, Padding, and Trimming functions -

This project is currently _NOT PRODUCTION_ ready.

This release adds the following methods

  • reverse()
  • pad()
  • truncate()

Also adds more inline documentation, Jazzy Docs, SwiftLint conformance, and more.

See build details and artifacts at buddybuild: https://dashboard.buddybuild.com/apps/58b67d22d21c470100b0c394/build/58b6e0894f23990100b72d7f

Initial Release for Dependency Managers. -

This project is currently _NOT PRODUCTION_ ready. The release was made to add support for Carthage and CocoaPods.

The following functions have been implemented, but not tested:

  • Boolean Operations
    • isAlpha()
    • isAlphanumeric()
    • isCapitalized()
    • isDecapitalized()
    • isLowercased()
    • isNumeric()
    • isUppercased()
  • Case Operations
    • camelCased()
    • decapitalized()
    • kebabCased()
    • pascalCased()
    • slug()
    • snakeCased()
    • swapCased()
  • Character Operations
    • first()
    • last()
  • Padding Operations
    • padLeft()
    • padRight()
  • Trimming Operations
    • prefixed()
    • suffixed()
    • trimLeft()
    • trimRight()