A string of characters. Each character in the string is a 32-bit Unicode character. The internal UTF-16 encoding is hidden from clients.

Literal strings may be written between double quotes:

"hello world"
"\r\n"
"\{#03C0} \{#2248} 3.14159"
"\{GREEK SMALL LETTER PI} \{ALMOST EQUAL TO} 3.14159"

Alternatively, a verbatim string may be written between tripled double quotes.

The empty string, "", is a string with no characters.

A string is a Category of its characters, and of its substrings:

'w' in greeting 
"hello" in greeting

Strings are summable:

String greeting = "hello" + " " + "world";

They are efficiently iterable:

for (char in "hello world") { ... }

They are lists of characters:

value char = "hello world"[5];

They are ranged:

String who = "hello world"[6...];

Note that since string[index] evaluates to the optional type Character?, it is often more convenient to write string[index..index], which evaluates to a String containing a single character, or to the empty string "" if index refers to a position outside the string.

It is easy to use comprehensions to transform strings:

String { for (s in "hello world") if (s.letter) s.uppercased }

Since a String has an underlying UTF-16 encoding, certain operations are expensive, requiring iteration of the characters of the string. In particular, size requires iteration of the whole string, and get(), span(), and measure() require iteration from the beginning of the string to the given index.

By: Gavin

no subtypes hierarchy

Initializer
String({Character*} characters)
Parameters:
  • characters

    The characters that form this string.

Attributes
coalescedSource Codeshared actual String coalesced

This string.

emptySource Codeshared actual Boolean empty

Determines if this string has no characters, that is, if it has zero size. This is a much more efficient operation than string.size==0.

See also size
Refines Collection.empty ultimately refines Iterable.empty
firstSource Codeshared actual Character? first

The first character in the string.

Refines List.first ultimately refines Iterable.first
hashSource Codeshared actual Integer hash

The hash value of the value, which allows the value to be an element of a hash-based set or key of a hash-based map. Implementations must respect the constraint that:

  • if x==y then x.hash==y.hash.

Therefore, a class which refines Object.equals() must also refine hash.

Refines List.hash ultimately refines Object.hash
keysSource Codeshared actual Integer[] keys

A sequence containing all indexes of this string.

Refines List.keys ultimately refines Correspondence.keys
lastSource Codeshared actual Character? last

The last character in the string.

Refines List.last ultimately refines Iterable.last
lastIndexSource Codeshared actual Integer? lastIndex

The index of the last character in the string, or null if the string has no characters. Note that this operation is potentially costly for long strings, since the underlying representation of the characters uses a UTF-16 encoding. For any nonempty string:

string.lastIndex == string.size-1
linesSource Codeshared {String*} lines

Split the string into lines of text, discarding line breaks. Recognized line break sequences are \n and \r\n.

linesWithBreaksSource Codeshared {String*} linesWithBreaks

Split the string into lines of text with line breaks. Each line will be terminated by a line break sequence, \n or \r\n.

See also lines
lowercasedSource Codeshared String lowercased

This string, with all characters in lowercase.

normalizedSource Codeshared String normalized

A string containing the characters of this string after collapsing strings of whitespace into single space characters and discarding whitespace from the beginning and end of the string.

restSource Codeshared actual String rest

The rest of the string, without its first character.

Refines List.rest ultimately refines Iterable.rest
reversedSource Codeshared actual String reversed

A string containing the characters of this string, with the characters in reverse order.

sizeSource Codeshared actual Integer size

The length of the string (the number of characters it contains). In the case of the empty string, the string has length zero. Note that this operation is potentially costly for long strings, since the underlying representation of the characters uses a UTF-16 encoding. Use of longerThan() or shorterThan() is highly recommended.

Refines List.size ultimately refines Iterable.size
stringSource Codeshared actual String string

This string.

Refines Collection.string ultimately refines Object.string
trimmedSource Codeshared String trimmed

A string containing the characters of this string, after discarding whitespace from the beginning and end of the string.

uppercasedSource Codeshared String uppercased

This string, with all characters in uppercase.

Inherited Attributes
Attributes inherited from: Collection<Element>
Attributes inherited from: Correspondence<Key,Item>
Attributes inherited from: Iterable<Element,Absent>
Attributes inherited from: List<Element>
Methods
cloneSource Codeshared actual String clone()

This string.

Refines List.clone ultimately refines Collection.clone
compareSource Codeshared actual Comparison compare(String other)

Compare this string with the given string lexicographically, according to the Unicode code points of the characters.

containsSource Codeshared actual Boolean contains(Object element)

Determines if the given object is a String and, if so, if it occurs as a substring of this string, or if the object is a Character that occurs in this string. That is to say, a string is considered a Category of its substrings and of its characters.

Refines List.contains ultimately refines Category.contains
definesSource Codeshared actual Boolean defines(Integer index)

Determines if this string contains a character at the given index, that is, if 0<=index<size.

Refines List.defines ultimately refines Correspondence.defines
endsWithSource Codeshared actual Boolean endsWith(List<Anything> substring)

Determine if the given list occurs at the end of this list.

equalsSource Codeshared actual Boolean equals(Object that)

Determines if the given object is a String, and if so, if this string has the same length, and the same characters, in the same order, as the given string.

Refines List.equals ultimately refines Object.equals
firstInclusionSource Codeshared actual Integer? firstInclusion(List<Anything> sublist)

The first index in this list at which the given list occurs as a sublist.

firstOccurrenceSource Codeshared actual Integer? firstOccurrence(Anything element)

The first index in this list at which the given element occurs.

getFromFirstSource Codeshared actual Character? getFromFirst(Integer index)

Returns the character at the given index in the string, or null if the index is before the start of the string or past the end of string. The first character in the string occurs at index zero. The last character in the string occurs at index string.size-1.

Refines List.getFromFirst ultimately refines Iterable.getFromFirst
getFromLastSource Codeshared actual Character? getFromLast(Integer index)

Get the character at the specified index, where the string is indexed from the end of the string, or null if the index falls outside the bounds of this string.

includesSource Codeshared actual Boolean includes(List<Anything> sublist)

Determine if the given list occurs as a sublist at some index in this list.

includesAtSource Codeshared actual Boolean includesAt(Integer index, List<Anything> sublist)

Determine if the given list occurs as a sublist at the given index of this list.

inclusionsSource Codeshared actual {Integer*} inclusions(List<Anything> sublist)

The indexes in this list at which the given list occurs as a sublist.

initialSource Codeshared actual String initial(Integer length)

Select the first characters of this string, returning a string no longer than the given length. If this string is shorter than the given length, return this string. Otherwise, return a string of the given length.

Refines List.initial
iteratorSource Codeshared actual Iterator<Character> iterator()

An iterator for the characters of the string.

Refines List.iterator ultimately refines Iterable.iterator
joinSource Codeshared String join({Object*} objects)

Join the string representations of the given objects, using this string as a separator.

largerThanSource Codeshared actual Boolean largerThan(String other)

Determines if this value is strictly larger than the given value.

lastInclusionSource Codeshared actual Integer? lastInclusion(List<Anything> sublist)

The last index in this list at which the given list occurs as a sublist.

lastOccurrenceSource Codeshared actual Integer? lastOccurrence(Anything element)

The last index in this list at which the given element occurs.

longerThanSource Codeshared actual Boolean longerThan(Integer length)

Determines if this string is longer than the given length. This is a more efficient operation than string.size>length.

See also size
Refines List.longerThan ultimately refines Iterable.longerThan
measureSource Codeshared actual String measure(Integer from, Integer length)

A string containing the characters of this string beginning at the given start index, returning a string no longer than the given length. If the portion of this string starting at the given index is shorter than the given length, return the portion of this string from the given index until the end of this string. Otherwise, return a string of the given length. If the start index is larger than the last index of the string, return the empty string.

Refines List.measure ultimately refines Ranged.measure
notLargerThanSource Codeshared actual Boolean notLargerThan(String other)

Determines if this value is smaller than or equal to the given value.

notSmallerThanSource Codeshared actual Boolean notSmallerThan(String other)

Determines if this value is larger than or equal to the given value.

occursSource Codeshared actual Boolean occurs(Anything element)

Determines if the given value occurs as an element of this list.

Refines List.occurs
occursAtSource Codeshared actual Boolean occursAt(Integer index, Anything element)

Determines if the given value occurs at the given index in this list.

padSource Codeshared String pad(Integer size, Character character = ...)

Pad this string with the given character, producing a string of the given minimum size, centering the string.

Parameters:
  • character = ' '

    The padding character

padLeadingSource Codeshared String padLeading(Integer size, Character character = ...)

Left pad this string with the given character, producing a string of the given minimum size.

Parameters:
  • character = ' '

    The padding character

padTrailingSource Codeshared String padTrailing(Integer size, Character character = ...)

Right pad this string with the given character, producing a string of the given minimum size.

Parameters:
  • character = ' '

    The padding character

plusSource Codeshared actual String plus(String other)

Returns the concatenation of this string with the given string.

repeatSource Codeshared actual String repeat(Integer times)

Returns a string formed by repeating this string the given number of times, or the empty string if times<=0.

Refines List.repeat ultimately refines Iterable.repeat
replaceSource Codeshared String replace(String substring, String replacement)

Returns a string formed by replacing every occurrence in this string of the given substring with the given replacement string, working from the start of this string to the end.

replaceFirstSource Codeshared String replaceFirst(String substring, String replacement)

Returns a string formed by replacing the first occurrence in this string of the given substring, if any, with the given replacement string.

replaceLastSource Codeshared String replaceLast(String substring, String replacement)

Returns a string formed by replacing the last occurrence in this string of the given substring, if any, with the given replacement string.

shorterThanSource Codeshared actual Boolean shorterThan(Integer length)

Determines if this string is shorter than the given length. This is a more efficient operation than string.size>length.

See also size
Refines List.shorterThan ultimately refines Iterable.shorterThan
sliceSource Codeshared actual [String, String] slice(Integer index)

Return two strings, the first containing the characters that occur before the given index, the second with the characters that occur after the given index. If the given index is outside the range of indices of this string, one of the returned strings will be empty.

Refines List.slice
smallerThanSource Codeshared actual Boolean smallerThan(String other)

Determines if this value is strictly smaller than the given value.

spanSource Codeshared actual String span(Integer from, Integer to)

A string containing the characters of this string between the given indexes. If the start index is the same as the end index, return a string with a single character. If the start index is larger than the end index, return the characters in the reverse order from the order in which they appear in this string. If both the start index and the end index are larger than the last index in the string, or if both the start index and the end index are smaller than the first index in the string, return the empty string. Otherwise, if the last index is larger than the last index in the string, return all characters from the start index to last character of the string.

Refines List.span ultimately refines Ranged.span
spanFromSource Codeshared actual String spanFrom(Integer from)

A string containing the characters of this string from the given start index inclusive to the end of the string. If the start index is larger than the last index of the string, return the empty string. If the start index is negative, return this string.

Refines List.spanFrom ultimately refines Ranged.spanFrom
spanToSource Codeshared actual String spanTo(Integer to)

A string containing the characters of this string from the start of the string up to and including the given end index. If the end index is negative, return the empty string. If the end index is larger than the last index in this string, return this string.

Refines List.spanTo ultimately refines Ranged.spanTo
splitSource Codeshared {String*} split(Boolean splitting(Character ch) = ..., Boolean discardSeparators = true, Boolean groupSeparators = true)

Split the string into tokens, using the given predicate function to determine which characters are separator characters.

value pathElements = path.split('/'.equals);

The flags discardSeparators and groupSeparators determine how separator characters should occur in the resulting stream.

Parameters:
  • splitting = ch.whitespace

    A predicate that determines if a character is a separator characters at which to split. Default to split at any whitespace character.

  • discardSeparators = true

    Specifies that the separator characters occurring in the string should be discarded. If false, they will be included in the resulting iterator.

  • groupSeparators = true

    Specifies that the separator tokens should be grouped eagerly and not be treated as single-character tokens. If false each separator token will be of size 1.

startsWithSource Codeshared actual Boolean startsWith(List<Anything> substring)

Determine if the given list occurs at the start of this list.

terminalSource Codeshared actual String terminal(Integer length)

Select the last characters of the string, returning a string no longer than the given length. If this string is shorter than the given length, return this string. Otherwise, return a string of the given length.

trimSource Codeshared actual String trim(Boolean trimming(Character elem))

A string containing the characters of this string, after discarding the characters matching the given predicate function from the beginning and end of the string.

value trimmed = name.trim('_'.equals);

A character is removed from the string if it matches the given predicate and if either:

  • every character occurring earlier in the string also matches the predicate, or
  • every character occurring later in the string also matches the predicate.
Parameters:
  • trimming

    The predicate function that determines whether a character should be trimmed

Refines List.trim
trimLeadingSource Codeshared actual String trimLeading(Boolean trimming(Character elem))

A string containing the characters of this string, after discarding the characters matching the given predicate function from the beginning of the string.

A character is removed from the string if it matches the given predicate and every character occurring earlier in the string also matches the predicate.

Parameters:
  • trimming

    The predicate function that determines whether a character should be trimmed

trimTrailingSource Codeshared actual String trimTrailing(Boolean trimming(Character elem))

A string containing the characters of this string, after discarding the characters matching the given predicate function from the end of the string.

A character is removed from the string if it matches the given predicate and every character occurring later in the string also matches the predicate.

Parameters:
  • trimming

    The predicate function that determines whether a character should be trimmed

Inherited Methods
Methods inherited from: Category<Element>
Methods inherited from: Collection<Element>
Methods inherited from: Comparable<Other>
Methods inherited from: Correspondence<Key,Item>
Methods inherited from: Iterable<Element,Absent>
Methods inherited from: List<Element>
Methods inherited from: Ranged<Index,Element,Subrange>
Methods inherited from: Summable<Other>