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"
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
.
Propertiesβ
byte_count: Int (get)β
The number of bytes this string stores.
is_empty: Bool (get)β
Whether the string has no characters in it.
length: Int (get)β
Number of unicode characters.
Methodsβ
appending(_ other: String) -> (String)β
Returns a new string with other
appended.
contains(_ other: String) -> (Bool)β
Returns whether other
is found in the string.
has_prefix(_ prefix: String) -> (Bool)β
Returns whether the string starts with the given prefix
.
has_suffix(_ suffix: String) -> (Bool)β
Returns whether the string ends with the given suffix
.
index(of other: String, from: Int = 0) -> (Int?)β
Returns the first index of other
if found, starting the search from
the given character.
inserting(_ other: String, at index: Int) -> (String)β
Returns a new string with other
inserted into the string at the given character index
.
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() -> (String)β
Returns a lowercase version of the string.
prefix(_ max_length: Int) -> (String)β
Returns a substring, up to the specified maximum length, starting from the beginning.
removing_all(_ other: String) -> (String)β
Returns a string with all occurences of other
removed.
removing_subrange(from: Int, length: Int? = nil) -> (String)β
Returns a string with the subrange removed.
Parametersβ
fromβ
The starting character index
lengthβ
The number of characters to remove. If not specified, the length will be the remainder after from
.
replacing_all(_ target: String, with replacement: String) -> (String)β
Returns a string with all matches of target
replaced by replacement
.
reversed() -> (String)β
Returns a reversed version of the string.
split(separator: String) -> ([String])β
Returns a list of all segments obtained from splitting the string at every separator
found in it.
substring(from: Int, length: Int? = nil) -> (String)β
Returns a string containing only the specified range.
Parametersβ
fromβ
The starting character index
lengthβ
The number of characters to include. If not specified, the length will be the remainder after from
.
suffix(_ max_length: Int) -> (String)β
Returns a substring, up to the specified maximum length, containing the final characters in the string.
trimmed() -> (String)β
Returns a version with all leading and trailing whitespace removed.
uppercased() -> (String)β
Returns an uppercase version of the string.