Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
Slipp Douglas Thompson edited this page Apr 20, 2017 · 20 revisions

Contents

Properties

length

Length of self.

Example

"Hello".length
// → 5

Instance methods

Sub-strings

at

  • at (indexes: Int...) -> String[]

Returns an array of characters at indexes in self.

Example

let str = "This is a string"
str.at(2, 3, 5)
// → ["i", "s", "i"]

Pattern matching

matches

  • matches (pattern: String, ignoreCase: Bool = false) -> NSTextCheckingResult[]?

Creates an NSRegularExpression object with pattern and returns all the matches in self.

Example

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

  • capitalized () -> String

self with the first character changed to its corresponding uppercase value.

Example

"ciao".capitalized()
// → Ciao

ltrimmed

  • ltrimmed () -> String

Strip whitespace from the beginning of a string.

Example

" \nCiao".ltrimmed()
// → Ciao

rtrimmed

  • rtrimmed () -> String

Strip whitespace from the end of a string.

Example

"Ciao \n".rtrimmed()
// → Ciao

trimmed

  • trimmed () -> String

Strip whitespace from the beginning and end of a string.

Example

" \nCiao \n".trimmed()
// → Ciao

insert

  • insert (index: Int, _ string: String) -> String

Inserts string before the character at the given index.

Example

"Heo".insert(2, "ll")
// → Hello

Misc

explode

  • explode (separator: Character) -> String[]

Returns an array of strings, each of which is a substring of self formed by splitting it on separator.

Example

"Hello World".explode(" ")
// → ["Hello", "World"]

Class methods

random

  • random (var length len: Int = 0, charset: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> String

Returns a string of length len using random characters from charset.

Example

String.random(6)
// → fi30Xw

Operators

Subscript

  • subscript (range: Range<Int>) -> String?

Returns a substring of self in the given range.

  • subscript (indexes: Int...) -> String[]

Equivalent to at.

  • subscript (index: Int) -> String?

Returns the char at position index in self.

Star

  • * (first: String, n: Int) -> String

Returns a new string by repeating first, n times.

Example

"Ab" * 3
// → AbAbAb

Matching

  • =~ (string: String, pattern: String) -> Bool
  • =~ (string: String, options: (pattern: String, ignoreCase: Bool)) -> Bool

Returns true if string matches pattern. If options.ignoreCase is specified and is false 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 in strings match pattern.

  • |~ (strings: String[], pattern: String) -> Bool
  • |~ (strings: String[], options: (pattern: String, ignoreCase: Bool)) -> Bool

Returns true if any string in strings matches pattern.