Strings

Strings are immutable, efficient, Unicode sequences of characters.

c singleton  String
  | c is-a?: Character

Creates a String containing a single character.

Example:

> $a singleton
"a"
s as: List  List
  | s is-a?: String

Convert string s to a List of Characters.

Example:

> "foo" as: List
[$f, $o, $o]
l to: String  String
  | l is-a?: List

Attempts to convert a list to a string. If the list does not contain only Characters, @list-not-homogenous is signaled.

Example:

> [$a, $b, $c] to: String
"abc"
> [$a, 1, $c] to: String
ERROR: <error @list-not-homogenous>
s length  Integer
  | s is-a?: String

Yields the length of the string.

Example:

> "abc" length
3
> "Hello, 世界!" print length
Hello, 世界!
10
s empty?  Boolean
  | s is-a?: String

Is the string s empty?

Example:

> "" empty?
True
> "no" empty?
False
s at: index  Character
  | s is-a?: String
  | index is-a?: Integer

Returns the string's character at index. If the index is out of bounds, @(out-of-bounds: index for-string: s) is raised.

Example:

> "abc" at: 1
$b
> "abc" at: 3
ERROR: <error @(out-of-bounds: 3 for-string: "abc")>
s head  any
  | s is-a?: String

Returns the first character of the string. If the string is empty, @empty-string is raised.

Example:

> "abc" head
$a
s last  any
  | s is-a?: String

Returns the last character of the string. If the string is empty, @empty-string is raised.

Example:

> "abc" last
$c
s init  String
  | s is-a?: String

Returns all but the last character of the string. If the string is empty, @empty-string is raised.

Example:

> "abc" init
"ab"
s tail  String
  | s is-a?: String

Returns all but the first character of the string. If the string is empty, @empty-string is raised.

Example:

> "abc" tail
"bc"