String
A collection of unicode characters.
We expect all strings to be UTF-8 encoded!
var message: String = "Hello, world π"
Comparing Stringsβ
var word1 = "Cat"
var word2 = "Dog"
print("\{word1 == word2}") // Prints "false"
print("\{word1 != word2}") // Prints "true"
Escape Sequencesβ
Escape sequences allow you to embed non-printable characters and characters which would otherwise conflict with the languages syntax. String literals may contain the following escape sequences:
\n: Line break\r: Carriage return (8.9)\\: Backslash\": Quote\u{030A}: Unicode codepoints (see next section for more information)\{<expr>}: Interpolation (see section interpolation)
Unicode Codepointsβ
To insert unicode characters you can also use unicode codepoints. A unicode codepoint is written as \u{<value>}, where <value> is a placeholder for the codepoint's hexadecimal value, e.g. \u{00c5} for an Γ
. You can insert characters in different normalizations forms. The character Γ
can also be represented in the decomposed form \u{0041}\u{030A}.
var calendar_in_swedish = "kalendera\u{030A}r" // kalenderΓ₯r
Note that codepoints such as \u{030A} are combined together with the leading character and are not treated as a separate characters. The length of this string is 10.
Interpolationβ
String interpolation allows you to embed the values of variables or expressions into a string literal.
var name = "John"
var age = 18
print("Hello \{name}, over 18: \{age > 18}")
Propertiesβ
byte_countβ
byte_count: Int { get }
The number of bytes this string stores.
is_emptyβ
is_empty: Bool { get }
Whether the string has no characters in it.
lengthβ
length: Int { get }
Number of unicode characters.
Methodsβ
appendingβ
appending(_ other: String) -> (String)
Returns a new string with other appended.
containsβ
contains(_ other: String) -> (Bool)
Returns whether other is found in the string.
has_prefixβ
has_prefix(_ prefix: String) -> (Bool)
Returns whether the string starts with the given prefix.
has_suffixβ
has_suffix(_ suffix: String) -> (Bool)
Returns whether the string ends with the given suffix.
indexβ
index(of other: String, from: Int = 0) -> (Int?)
Returns the first index of other if found, starting the search from the given character.
insertingβ
inserting(_ other: String, at index: Int) -> (String)
Returns a new string with other inserted into the string at the given character index.
lexicographically_precedesβ
lexicographically_precedes(_ other: String) -> (Bool)
Returns whether the string should appear before other in a list.
This function doesn't implement proper ordering for all languages. It only works well for the latin alphabet.
lowercasedβ
lowercased() -> (String)
Returns a lowercase version of the string.
prefixβ
prefix(_ max_length: Int) -> (String)
Returns a substring, up to the specified maximum length, starting from the beginning.
removing_allβ
removing_all(_ other: String) -> (String)
Returns a string with all occurrences of other removed.
removing_subrangeβ
removing_subrange(from: Int, length: Int? = nil) -> (String)
Returns a string with the subrange removed.
from: IntThe starting character index
length: Int? = nilThe number of characters to remove. If not specified, the length will be the remainder after
from.
replacing_allβ
replacing_all(_ target: String, with replacement: String) -> (String)
Returns a string with all matches of target replaced by replacement.
reversedβ
reversed() -> (String)
Returns a reversed version of the string.
splitβ
split(separator: String) -> ([String])
Returns a list of all segments obtained from splitting the string at every separator found in it.
substringβ
substring(from: Int, length: Int? = nil) -> (String)
Returns a string containing only the specified range.
from: IntThe starting character index
length: Int? = nilThe number of characters to include. If not specified, the length will be the remainder after
from.
suffixβ
suffix(_ max_length: Int) -> (String)
Returns a substring, up to the specified maximum length, containing the final characters in the string.
trimmedβ
trimmed() -> (String)
Returns a version with all leading and trailing whitespace removed.
uppercasedβ
uppercased() -> (String)
Returns an uppercase version of the string.