-
Notifications
You must be signed in to change notification settings - Fork 313
String
- Properties
- Instance methods
- Class methods
- Operators
#Properties
Length of
self.
"Hello".length
// → 5
#Instance methods ##Sub-strings
at (indexes: Int...) -> String[]
Returns an array of characters at
indexesinself.
let str = "This is a string"
str.at(2, 3, 5)
// → ["i", "s", "i"]
##Pattern matching
matches (pattern: String, ignoreCase: Bool = false) -> NSTextCheckingResult[]?
Creates an
NSRegularExpressionobject withpatternand 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
##Editing
capitalized () -> String
selfwith 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
stringbefore the character at the givenindex.
"Heo".insert(2, "ll")
// → Hello
##Misc
explode (separator: Character) -> String[]
Returns an array of strings, each of which is a substring of
selfformed by splitting it onseparator.
"Hello World".explode(" ")
// → ["Hello", "World"]
#Class methods
random (var length len: Int = 0, charset: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> String
Returns a string of length
lenusing random characters fromcharset.
String.random(6)
// → fi30Xw
#Operators ##Subscript
subscript (range: Range<Int>) -> String?
Returns a substring of
selfin the givenrange.
subscript (indexes: Int...) -> String[]
Equivalent to
at.
subscript (index: Int) -> String?
Returns the char at position
indexinself.
##Star
* (first: String, n: Int) -> String
Returns a new string by repeating
first,ntimes.
####Example
"Ab" * 3
// → AbAbAb
##Matching
=~ (string: String, pattern: String) -> Bool=~ (string: String, options: (pattern: String, ignoreCase: Bool)) -> Bool
Returns
trueifstringmatchespattern. Ifoptions.ignoreCaseis specified and isfalsethe matching is done case-sensitively.
=~ (strings: String[], pattern: String) -> Bool=~ (strings: String[], options: (pattern: String, ignoreCase: Bool)) -> Bool
Returns
trueif all the strings instringsmatchpattern.
|~ (strings: String[], pattern: String) -> Bool|~ (strings: String[], options: (pattern: String, ignoreCase: Bool)) -> Bool
Returns
trueif any string instringsmatchespattern.