Skip to content
Noodle
InstallLearnPlayground
GitHub

Strings and characters

String is immutable Unicode text. Char is one Unicode code point. They are distinct types: a one-character string is not a Char, and a Char is not an Int even though the JavaScript backend stores its code-point value as a number.

Quoted strings use "...", characters use single quotes, and ++ concatenates strings:

func greeting(name : String) -> String do
"Hello, " ++ name ++ "!"
end

Use String.join(values, separator) to combine an array of strings without a manual concatenation loop:

func main() -> Unit do
Console.println(String.join(["a", "b", "c"], ", "));
end

Associated conversion methods include:

  • Int.to_string();
  • Int.to_double();
  • Bool.to_int() and Bool.to_string();
  • Char.to_int() and Char.to_string();
  • Double.to_string().

Conversions are explicit; context never silently changes one primitive type into another.

Char.from_codepoint(value) constructs a character from an integer code point:

func main() -> Unit do
character = Char.from_codepoint(0x1f600);
Console.println(character.to_string());
Debug.trace(character.to_int());
end

Strings do not expose integer indexing or a code-point length() method. Instead, a StringCursor represents a boundary in one specific string:

func first_character(value : String) -> Option[Char] do
value.character_at(value.start_cursor())
end

The cursor API includes:

  • start_cursor() and end_cursor();
  • next_cursor(cursor) and previous_cursor(cursor), returning Option at the boundaries;
  • character_at(cursor) and unsafe_character_at(cursor);
  • slice(from, to) for a half-open cursor range;
  • utf16_distance(from, to) for protocols that need UTF-16 offsets.

A cursor belongs to the string from which it was obtained. Passing it to a different string is outside the API contract.

The standard library supplies an iterable provider for String, so foreach visits characters rather than backend UTF-16 code units:

func print_characters(value : String) -> Unit do
foreach character in value do
Console.println(character.to_string());
end;
end

This keeps supplementary Unicode characters such as '😀' as one Char.

The Double view provides sqrt(), pow(exponent), floor_to_int(), and truncate_to_int(). The last two are explicit lossy conversions with different rounding rules; neither is an implicit numeric conversion.

Next: Sorted maps.