Skip to main content

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.