String
struct String
-
A Boolean value indicating if all the characters are letters.
Declaration
Swift
func isAlpha() -> Bool
Return Value
true
, if all characters are letters. Otherwise,false
. -
A Boolean value indicating if all the characters are alphanumeric.
Declaration
Swift
func isAlphanumeric() -> Bool
Return Value
true
, if all characters are alphanumeric. Otherwise,false
. -
A Boolean value indicating if the first characters in all of the words in the string are uppercased.
Declaration
Swift
func isCapitalized() -> Bool
Return Value
true
, if the string is capitalized. Otherwise,false
. -
A Boolean value indicating if the first characters in all of the words in the string are lowercased.
Declaration
Swift
func isDecapitalized() -> Bool
Return Value
true
, if first character is lowercased. Otherwise,false
. -
A Boolean value indicating if all the characters are lowercased.
Declaration
Swift
func isLowercased() -> Bool
Return Value
true
, if the string is not capitalized. Otherwise,false
. -
A Boolean value indicating if all the characters are numbers.
Declaration
Swift
func isNumeric() -> Bool
Return Value
true
, if all characters are numbers. Otherwise,false
. -
A Boolean value indicating if all the characters are uppercased.
Declaration
Swift
func isUppercased() -> Bool
Return Value
true
, if all characters are uppercased. Otherwise,false
.
-
Returns a camel cased version of the string.
let string = "HelloWorld" print(string.camelCased()) // Prints "helloWorld"
Declaration
Swift
func camelCased() -> String
Return Value
A camel cased copy of the string.
-
Returns a capitalized version of the string.
Warning
This method is a modified implementation of Swift stdlib’s
capitalized
computer variabled.let string =
hello World
print(string.capitalized()) // PrintsHello World
Declaration
Swift
func capitalized() -> String
Return Value
A capitalized copy of the string.
-
Returns a decapitalized version of the string.
let string = "Hello World" print(string.decapitalized()) // Prints "hello world"
Declaration
Swift
func decapitalized() -> String
Return Value
A decapitalized copy of the string.
-
Returns the kebab cased version of the string.
let string = "Hello World" print(string.kebabCased()) // Prints "-hello-world-"
Declaration
Swift
func kebabCased() -> String
Return Value
The kebab cased copy of the string.
-
Returns a pascal cased version of the string.
let string = "HELLO WORLD" print(string.pascalCased()) // Prints "HelloWorld"
Declaration
Swift
func pascalCased() -> String
Return Value
A pascal cased copy of the string.
-
Returns the slug cased version of the string.
let string = "Hello World" print(string.slugCased()) // Prints "hello-world"
Declaration
Swift
func slugCased() -> String
Return Value
The slug cased copy of the string.
-
Returns the snake cased version of the string.
let string = "Hello World" print(string.snakeCased()) // Prints "hello_world"
Declaration
Swift
func snakeCased() -> String
Return Value
The snaked cased copy of the string.
-
Splits a string into mutliple words, delimited by uppercase letters.
let string = "HelloWorld" print(string.splitWordsByCase()) // Prints "Hello World"
Declaration
Swift
func splitWordsByCase() -> String
Return Value
A new string where each word in the original string is delimited by a whitespace.
-
Returns the swap cased version of the string.
let string = "Hello World" print(string.swapCased()) // Prints "hELLO wORLD"
Declaration
Swift
func swapCased() -> String
Return Value
The swap cased copy of the string.
-
Returns the first character of the string.
let string = "Hello World" print(string.first()) // Prints "H"
Declaration
Swift
func first() -> String
Return Value
The first character of the string.
-
Returns the last character of the string.
let string = "Hello World" print(string.last()) // Prints "d"
Declaration
Swift
func last() -> String
Return Value
The last character of the string.
-
Returns the character count of the string.
let string = "Hello World" print(string.length()) // Prints 11
Declaration
Swift
func length() -> Int
Return Value
The character count of the string.
-
Retuns the reversed version of the string.
let string = "Hello World" print(string.reversed()) // Prints "dlroW olleH"
Declaration
Swift
func reversed() -> String
Return Value
The reversed copy of the string.
-
Returns the center-padded version of the string.
Example 1:
let string = "Hello World" print(string.pad(length: 13)) // Prints " Hello World "
Example 2:
let string = "Hello World" print(string.pad(length: 13, withToken: "*")) // Prints "*Hello World*"
Declaration
Swift
func pad(length: Int, withToken token: String = " ") -> String
Parameters
length
The final length of your string. If the provided length is less than or equal to the original string, the original string is returned. If the the sum-total of characters added is odd, the left side of the string will have one less instance of the token.
token
The string used to pad the String. Must be 1 character in length. Defaults to a white space if the parameter is left blank.
Return Value
The padded copy of the string.
-
Returns the left-padded version of the string.
Example 1:
let string = "Hello World" print(string.padLeft(length: 13)) // Prints " Hello World"
Example 2:
let string = "Hello World" print(string.padLeft(length: 13, withToken: "*")) // Prints "**Hello World"
Declaration
Swift
func padLeft(length: Int, withToken token: String = " ") -> String
Parameters
length
The final length of your string. If the provided length is less than or equal to the original string, the original string is returned.
token
The string used to pad the String. Must be 1 character in length. Defaults to a white space if the parameter is left blank.
Return Value
The left-padded copy of the string.
-
Returns the right-padded version of the string.
Example 1:
let string = "Hello World" print(string.padRight(length: 13)) // Prints "Hello World "
Example 2:
let string = "Hello World" print(string.padRight(length: 13, withToken: "*", )) // Prints "Hello World**"
Declaration
Swift
func padRight(length: Int, withToken token: String = " ") -> String
Parameters
length
The final length of your string. If the provided length is less than or equal to the original string, the original string is returned.
token
The string used to pad the String. Must be 1 character in length. Defaults to a white space if the parameter is left blank.
Return Value
The right-padded copy of the string.
-
Returns a prefixed version of the string.
let string = "Hello World" print(trimLeft(byKeeping: 7)) // Prints "Hello W"
Declaration
Swift
func trimLeft(byKeeping length: Int) -> String
Parameters
length
The length of the string that you’d like to return, starting at the beginning of the string. If the provided length is greater than the original string, the original string is returned.
Return Value
A prefixed copy of the string.
-
Returns a suffixed version of the string.
let string = "Hello World" print(trimRight(byKeeping: 7)) // Prints "o World"
Declaration
Swift
func trimRight(byKeeping length: Int) -> String
Parameters
length
The length of the string that you’d like to return, starting at the end of the string. If the provided length is greater than the original string, the original string is returned.
Return Value
A prefixed copy of the string.
-
Returns the left-trimmed version of the string.
Example:
let string = "Hello World" print(string.trimLeft(byRemoving: length: 7)) // Prints "o World"
Declaration
Swift
func trimLeft(byRemoving length: Int) -> String
Parameters
length
The number of characters to trim from the beginning of the string. If the provided length is greater than the original string, the original string is returned.
Return Value
The left-trimmed copy of the string.
-
Returns the right-trimmed version of the string.
Example 1:
let string = "Hello World" print(string.trimRight(byRemoving: length: 7)) // Prints "Hello W"
Declaration
Swift
func trimRight(byRemoving length: Int) -> String
Parameters
length
The number of characters to trim from the end of the string. If the provided length is greater than the original string, the original string is returned.
Return Value
The right-trimmed copy of the string.
-
Returns the truncated string with an ellipsis.
Example:
let string = "Hello World" print(string.truncated(length: 8)) // Prints "Hello..."
Declaration
Swift
func truncated(length: Int) -> String
Parameters
length
The final length of the string, which includes the ellipsis:
...
).Return Value
The truncated copy of the string.