ddonche/goblin-lang
0.46.24
1
0
docs reference
[[chars]]

Chars


chars separates a string into an array of characters.


Overview

Use chars when you want to work with each character in a string separately:

letters | "Goblin".chars

:say(letters)  /// ['G', 'o', 'b', 'l', 'i', 'n']
letters | "Goblin".chars

:say(letters)  /// ['G', 'o', 'b', 'l', 'i', 'n']

Each item in the returned array is a char, not a one-character string.


Method Style

Call chars directly on a string:

symbols | "cave!".chars
symbols | "cave!".chars

Because chars needs no additional arguments, the method form does not need parentheses.

The equivalent free-call form is:

symbols | chars("cave!")
symbols | chars("cave!")

Both forms behave the same way.


Spaces and Punctuation

Spaces, punctuation, and other visible characters become their own array items:

"hi there!".chars
/// ['h', 'i', ' ', 't', 'h', 'e', 'r', 'e', '!']
"hi there!".chars
/// ['h', 'i', ' ', 't', 'h', 'e', 'r', 'e', '!']

chars does not remove or alter any characters.


Unicode Characters

chars separates text by Unicode characters rather than raw bytes:

"café".chars
/// ['c', 'a', 'f', 'é']
"café".chars
/// ['c', 'a', 'f', 'é']

This means a character such as é is returned as one char, even though its encoded representation may use more than one byte.


Empty Strings

An empty string produces an empty array:

"".chars  /// []
"".chars  /// []


Characters, Not Strings

The returned values have the char type:

first | "Goblin".chars[0]

:say(first.vt)  /// char
first | "Goblin".chars[0]

:say(first.vt)  /// char

This differs from some string operations that produce an array of strings. Use Join when you want to combine characters into a string again:

letters | "Goblin".chars
word | letters.join("")

:say(word)  /// Goblin
letters | "Goblin".chars
word | letters.join("")

:say(word)  /// Goblin


Signature

string.chars
chars(string)
string.chars
chars(string)

Argument Type Description
string string The text to separate into characters.

chars returns an array of char values.


Errors

chars requires exactly one string:

chars()
/// error: R0301 wrong-arity
chars()
/// error: R0301 wrong-arity

The value must be a string:

chars(1234)
/// error: T0205 type-mismatch
chars(1234)
/// error: T0205 type-mismatch