All Projects → iamjono → SwiftString

iamjono / SwiftString

Licence: MIT license
A comprehensive, lightweight string extension for Swift 3.x & 4.0

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to SwiftString

Perfect-HTTPServer
HTTP server for Perfect.
Stars: ✭ 104 (-11.11%)
Mutual labels:  perfect, server-side-swift
bugsnag
Report errors with Bugsnag 🐛
Stars: ✭ 37 (-68.38%)
Mutual labels:  vapor, server-side-swift
readme
Welcome to Vapor development at Nodes 📖
Stars: ✭ 47 (-59.83%)
Mutual labels:  vapor, server-side-swift
routing-kit
🚍 High-performance trie-node router.
Stars: ✭ 95 (-18.8%)
Mutual labels:  vapor, server-side-swift
Perfect-Thread
Core threading library for Perfect Server Side Swift. Includes support for serial and concurrent thread queues, locks, read/write locks and events.
Stars: ✭ 17 (-85.47%)
Mutual labels:  perfect, server-side-swift
Perfect-Weather
Demonstrate using URL Routes & variables, Fetching of remote data from API's as JSON, reading and transforming to data more appropriately consumable by an API client.
Stars: ✭ 32 (-72.65%)
Mutual labels:  perfect, server-side-swift
auth
👤 Authentication and Authorization framework for Fluent.
Stars: ✭ 51 (-56.41%)
Mutual labels:  vapor, server-side-swift
fluent-postgres-driver
🐘 PostgreSQL driver for Fluent.
Stars: ✭ 120 (+2.56%)
Mutual labels:  vapor, server-side-swift
flash
Flash messages between views ⚡️
Stars: ✭ 34 (-70.94%)
Mutual labels:  vapor, server-side-swift
Stevenson
Stevenson is a Vapor framework designed to build integrations between Slack apps, GitHub, JIRA and CI services (CircleCI).
Stars: ✭ 57 (-51.28%)
Mutual labels:  vapor, server-side-swift
gatekeeper
Rate limiting middleware for Vapor 👮
Stars: ✭ 54 (-53.85%)
Mutual labels:  vapor, server-side-swift
Perfect-URLRouting
Perfect Example Module: URL Routing
Stars: ✭ 20 (-82.91%)
Mutual labels:  perfect, server-side-swift
Perfect-HTTP
Base HTTP Support for Perfect.
Stars: ✭ 29 (-75.21%)
Mutual labels:  perfect, server-side-swift
GraphQLRouteCollection
A GraphQL based RouteCollection for Vapor
Stars: ✭ 18 (-84.62%)
Mutual labels:  vapor, server-side-swift
async
⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
Stars: ✭ 35 (-70.09%)
Mutual labels:  vapor, server-side-swift
HomeKitty
A Vapor 3 website to easily browse HomeKit accessories.
Stars: ✭ 75 (-35.9%)
Mutual labels:  vapor, server-side-swift
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (-42.74%)
Mutual labels:  vapor, server-side-swift
fluent-mysql-driver
🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
Stars: ✭ 69 (-41.03%)
Mutual labels:  vapor, server-side-swift
Perfect-SMTP
SMTP Client for Perfect.
Stars: ✭ 19 (-83.76%)
Mutual labels:  perfect, server-side-swift
Perfect-XML
XML support for Perfect.
Stars: ✭ 16 (-86.32%)
Mutual labels:  perfect, server-side-swift

SwiftString

Version License Language Language

SwiftString is a lightweight string extension for Swift 3 and 4. This library was motivated by having to search StackOverflow for common string operations, and wanting them to be in one place with test coverage.

Note the original client side Swift 2 repo can be found here: https://github.com/amayne/SwiftString

This Fork

This fork is intended as a server side utility.

  • It is Swift 3.x, 4.0 and Swift Package Manager (SPM) ready.
  • Added sigificant test coverage

Swift 3.0.2, 3.1 only

If you are targeting Swift 3.0.2 or 3.1 only, please use the majorVersion:1 in your Package.swift file:

.Package(url: "https://github.com/iamjono/SwiftString.git", majorVersion: 1),

This release works for both Swift 3.0.2 and 3.1, on macOS and Linux.

Swift 3.0.2, 3.1, 3.2 and Swift 4.0

If you have updated to Swift 4, or plan to in the near future, use the majorVersion:2 in your Package.swift file. This version brings compatibility with Swift 4, and is backwards compatible with the 3.x codebases.

.Package(url: "https://github.com/iamjono/SwiftString.git", majorVersion: 2),

The latest release works for Swift 3.x and Swift 4 on macOS and Linux.

Usage

import SwiftString

Methods

between(left, right)

"<a>foo</a>".between("<a>", "</a>") // "foo"
"<a><a>foo</a></a>".between("<a>", "</a>") // "<a>foo</a>"
"<a>foo".between("<a>", "</a>") // nil
"Some strings } are very {weird}, dont you think?".between("{", "}") // "weird"
"<a></a>".between("<a>", "</a>") // nil
 "<a>foo</a>".between("<a>", "<a>") // nil

camelize()

"os version".camelize() // "osVersion"
"HelloWorld".camelize() // "helloWorld"
"someword With Characters".camelize() // "somewordWithCharacters"
"data_rate".camelize() // "dataRate"
"background-color".camelize() // "backgroundColor"

capitalize()

"hello world".capitalize() // "Hello World"

chompLeft(string)

"foobar".chompLeft("foo") // "bar"
"foobar".chompLeft("bar") // "foo"

chompRight(string)

"foobar".chompRight("bar") // "foo"
"foobar".chompRight("foo") // "bar"

cleanPath()

var foo = "hello//world/..///stuff.txt"
foo.cleanPath() // foo == "hello/stuff.txt"

cleanedPath()

"hello//world/..///stuff.txt".cleanedPath() // "hello/stuff.txt"

collapseWhitespace()

"  String   \t libraries are   \n\n\t fun\n!  ".collapseWhitespace() // "String libraries are fun !")

count(string)

"hi hi ho hey hihey".count("hi") // 3

decodeHTML()

"The Weekend &#8216;King Of The Fall&#8217;".decodeHTML() // "The Weekend ‘King Of The Fall’"
"<strong> 4 &lt; 5 &amp; 3 &gt; 2 .</strong> Price: 12 &#x20ac;.  &#64; ".decodeHTML() // "<strong> 4 < 5 & 3 > 2 .</strong> Price: 12 €.  @ "
"this is so &quot;good&quot;".decodeHTML() // "this is so \"good\""

endsWith(suffix)

"hello world".endsWith("world") // true
"hello world".endsWith("foo") // false

ensureLeft(prefix)

"/subdir".ensureLeft("/") // "/subdir"
"subdir".ensureLeft("/") // "/subdir"

ensureRight(suffix)

"subdir/".ensureRight("/") // "subdir/"
"subdir".ensureRight("/") // "subdir/"

extension

"/hello/world.txt".extension // "txt"
"/hello/world.tmp.txt".extension // "txt"

file

"/hello/world.txt".file // "world.txt"
"/hello/there/".file // "there"

fileName

"/hello/world.txt".fileName // "world"
"/hello/there/".fileName // "there"

index(of: substring)

"hello".index(of: "hell"), // 0
"hello".index(of: "lo"), // 3
"hello".index(of: "world") // -1
"hellohello".index(of: "hel", after: 2) // 5

indexOf(substring) - deprecated in favor of index(of:) above

"hello".indexOf("hell"), // 0
"hello".indexOf("lo"), // 3
"hello".indexOf("world") // nil

initials()

"First".initials(), // "F"
"First Last".initials(), // "FL"
"First Middle1 Middle2 Middle3 Last".initials() // "FMMML"

initialsFirstAndLast()

"First Last".initialsFirstAndLast(), // "FL"
"First Middle1 Middle2 Middle3 Last".initialsFirstAndLast() // "FL"

isAlpha()

"fdafaf3".isAlpha() // false
"afaf".isAlpha() // true
"dfdf--dfd".isAlpha() // false

isAlphaNumeric()

"afaf35353afaf".isAlphaNumeric() // true
"FFFF99fff".isAlphaNumeric() // true
"99".isAlphaNumeric() // true
"afff".isAlphaNumeric() // true
"-33".isAlphaNumeric() // false
"aaff..".isAlphaNumeric() // false

isEmpty()

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

isNumeric()

"abc".isNumeric() // false
"123a".isNumeric() // false
"1".isNumeric() // true
"22".isNumeric() // true
"33.0".isNumeric() // true
"-63.0".isNumeric() // true

join(paths...)

var root = "/foo"
root.join("bar", "/baz", "..", "//somedata.txt") // root == "/foo/bar/somedata.txt"

var root = "/foo"
root.join(paths: ["bar", "/baz", "..", "//somedata.txt"]) // root == "/foo/bar/somedata.txt"

joining(paths...)

"/foo".joining("bar", "/baz", "..", "//somedata.txt") // "/foo/bar/somedata.txt"
"/foo".joining(paths: ["bar", "/baz", "..", "//somedata.txt"]) // "/foo/bar/somedata.txt"

lastIndex(of: substring)

"hellohellohello".lastIndex(of: "hell"), // 10
"hellohellohello".lastIndex(of: "lo"), // 13
"hellohellohello".lastIndex(of: "world") // -1
"hellohellohello".lastIndex(of: "hel", before: 10) // 5

latinize()

"šÜįéïöç".latinize() // "sUieioc"
"crème brûlée".latinize() // "creme brulee"

lines()

"test".lines() // ["test"]
"test\nsentence".lines() // ["test", "sentence"]
"test \nsentence".lines() // ["test ", "sentence"]

pad(n, string)

"hello".pad(2) // "  hello  "
"hello".pad(1, "\t") // "\thello\t"

padLeft(n, string)

"hello".padLeft(10) // "          hello"
"what?".padLeft(2, "!") // "!!what?"

padRight(n, string)

"hello".padRight(10) // "hello          "
"hello".padRight(2, "!") // "hello!!"

parent

"/hello/there/world.txt".parent // "/hello/there"
"/hello/there".parent // "/hello"

startsWith(prefix)

"hello world".startsWith("hello") // true
"hello world".startsWith("foo") // false

split(separator)

"hello world".split(" ")[0] // "hello"
"hello world".split(" ")[1] // "world"
"helloworld".split(" ")[0] // "helloworld"

times(n)

"hi".times(3) // "hihihi"
" ".times(10) // "          "

toBool()

"asdwads".toBool() // nil
"true".toBool() // true
"false".toBool() // false

toFloat()

"asdwads".toFloat() // nil
"2.00".toFloat() // 2.0
"2".toFloat() // 2.0

toInt()

"asdwads".toInt() // nil
"2.00".toInt() // 2
"2".toInt() // 2

toDate()

"asdwads".toDate() // nil
"2014-06-03".toDate() // NSDate

toDateTime()

"asdwads".toDateTime() // nil
"2014-06-03 13:15:01".toDateTime() // NSDate

toDouble()

"asdwads".toDouble() // nil
"2.00".toDouble() // 2.0
"2".toDouble() // 2.0

trimmedLeft()

"        How are you? ".trimmedLeft() // "How are you? "

trimmedRight()

" How are you?   ".trimmedRight() // " How are you?"

trimmed()

"    How are you?   ".trimmed() // "How are you?"

slugify()

"Global Thermonuclear Warfare".slugify() // "global-thermonuclear-warfare"
"Crème brûlée".slugify() // "creme-brulee"

stripPunctuation()

"My, st[ring] *full* of %punct)".stripPunctuation() // "My string full of punct"

substring(startIndex, length)

"hello world".substring(0, length: 1) // "h"
"hello world".substring(0, length: 11) // "hello world"

[subscript]

"hello world"[0...1] // "he"
"hello world"[0..<1] // "h"
"hello world"[0] // "h"
"hello world"[0...10] // "hello world"
"hello world"[safe: -1...1] // "he"
"hello world"[safe: 9...20] // "ld"

Requirements

  • Swift version 3.x or Swift 4. Please see notes above.

Installation

Install via Swift Package Manager

  • Add the following to your Package.swift file:
.Package(
    url: "https://github.com/iamjono/SwiftString.git",
    majorVersion: 2
    ),

Then, regenerate your Xcode project:

swift package generate-xcodeproj

Install via Cocoapods

pod "String+Extensions"

Author

Andrew Mayne, [email protected]

Swift 3 & 4 SPM module, Jonathan Guthrie, [email protected]

Cocoapods, Koji Murata, [email protected]

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].