解决Swift操作字符串的诟病

icons8-swift-128.png

JGString

JGString is a lightweight string extension for Swift.

Installation

JGString is available through CocoaPods. To install

it, simply add the following line to your Podfile:


pod "JGString"

Usage


import JGString

Methods

between(left, right)


"foo".between(left: "", "") // "foo"

"foo".between(left: "", "") // "foo"

"foo".between(left: "", "") // nil

"Some strings } are very {weird}, dont you think?".between(left: "{", "}") // "weird"

"".between(left: "", "") // nil

"foo".between(left: "", "") // 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(prefix: "foo") // "bar"

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

chompRight(string)


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

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

collapseWhitespace()


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

contains(substring)


"foobar".contains("foo") // true

"foobar".contains("bar") // true

"foobar".contains("something") // false

count(string)


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

decodeHTML()


"The Weekend ‘King Of The Fall’".decodeHTML() // "The Weekend ‘King Of The Fall’"

" 4 < 5 & 3 > 2 . Price: 12 €.  @ ".decodeHTML() // " 4 < 5 & 3 > 2 . Price: 12 €.  @ "

"this is so "good"".decodeHTML() // "this is so \"good\""

endsWith(suffix)


"hello world".endsWith(suffix: "world") // true

"hello world".endsWith(suffix: "foo") // false

ensureLeft(prefix)


"/subdir".ensureLeft(prefix: "/") // "/subdir"

"subdir".ensureLeft(prefix: "/") // "/subdir"

ensureRight(suffix)


"subdir/".ensureRight(suffix: "/") // "subdir/"

"subdir".ensureRight(suffix: "/") // "subdir/"

indexOf(substring)


"hello".indexOf(substring: "hell"), // 0

"hello".indexOf(substring: "lo"), // 3

"hello".indexOf(substring: "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(sequence)


",".join(elements: [1,2,3]) // "1,2,3"

",".join(elements: []) // ""

",".join(elements: ["a","b","c"]) // "a,b,c"

"! ".join(elements: ["hey","who are you?"]) // "hey! who are you?"

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!!"

startsWith(prefix)


"hello world".startsWith(prefix: "hello") // true

"hello world".startsWith(prefix: "foo") // false

split(separator)


"hello world".split(separator: " ")[0] // "hello"

"hello world".split(separator: " ")[1] // "world"

"helloworld".split(separator: " ")[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(startIndex: 0, length: 1) // "h"

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

[subscript]


""hello world"[0..<2] == "he"" // "he"

""hello world"[0..<1] == "h"" // "h"

""hello world"[0] == "h""

""hello world"[0..<11] == "hello world"" // "hello world"

你可能感兴趣的:(解决Swift操作字符串的诟病)