-
Notifications
You must be signed in to change notification settings - Fork 317
String
- Properties
- Instance methods
- Class methods
- Operators
Length of
self
.
"Hello".length
// → 5
at (indexes: Int...) -> String[]
Returns an array of characters at
indexes
inself
.
let str = "This is a string"
str.at(2, 3, 5)
// → ["i", "s", "i"]
matches (pattern: String, ignoreCase: Bool = false) -> NSTextCheckingResult[]?
Creates an
NSRegularExpression
object withpattern
and returns all the matches inself
.
let string = "AB[31]"
let matches = string.matches("\\d+")!
let range = matches[0].rangeAtIndex(0)
string[range.location...(range.location + range.length)]
// → 31
capitalized () -> String
self
with the first character changed to its corresponding uppercase value.
"ciao".capitalized()
// → Ciao
ltrimmed () -> String
Strip whitespace from the beginning of a string.
" \nCiao".ltrimmed()
// → Ciao
rtrimmed () -> String
Strip whitespace from the end of a string.
"Ciao \n".rtrimmed()
// → Ciao
trimmed () -> String
Strip whitespace from the beginning and end of a string.
" \nCiao \n".trimmed()
// → Ciao
insert (index: Int, _ string: String) -> String
Inserts
string
before the character at the givenindex
.
"Heo".insert(2, "ll")
// → Hello
explode (separator: Character) -> String[]
Returns an array of strings, each of which is a substring of
self
formed by splitting it onseparator
.
"Hello World".explode(" ")
// → ["Hello", "World"]
random (var length len: Int = 0, charset: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> String
Returns a string of length
len
using random characters fromcharset
.
String.random(6)
// → fi30Xw
subscript (range: Range<Int>) -> String?
Returns a substring of
self
in the givenrange
.
subscript (indexes: Int...) -> String[]
Equivalent to
at
.
subscript (index: Int) -> String?
Returns the char at position
index
inself
.
* (first: String, n: Int) -> String
Returns a new string by repeating
first
,n
times.
"Ab" * 3
// → AbAbAb
=~ (string: String, pattern: String) -> Bool
=~ (string: String, options: (pattern: String, ignoreCase: Bool)) -> Bool
Returns
true
ifstring
matchespattern
. Ifoptions.ignoreCase
is specified and isfalse
the matching is done case-sensitively.
=~ (strings: String[], pattern: String) -> Bool
=~ (strings: String[], options: (pattern: String, ignoreCase: Bool)) -> Bool
Returns
true
if all the strings instrings
matchpattern
.
|~ (strings: String[], pattern: String) -> Bool
|~ (strings: String[], options: (pattern: String, ignoreCase: Bool)) -> Bool
Returns
true
if any string instrings
matchespattern
.