Antra

Standardbibliothek

Antra::String

import Antra::String

String operations for Antra.

Core operations delegate to Antra::Native::String, while small compositions are implemented directly in Antra.

Überblick#

Für String-Helfer über die Basis-Built-ins hinaus.

Zeichen und Unicode-Codepoints lassen sich in beide Richtungen umwandeln:

import Antra::String

println(String.codepoint("€"))                  # 8364
println(String.from_codepoint(945))             # α
println(String.codepoints("hé"))                # [104, 233]
println(String.from_codepoints([72, 105]))      # Hi

Namen lassen sich zwischen den Schreibweisen umwandeln. Wörter trennen alle Zeichen außer Buchstaben und Ziffern sowie der Wechsel von klein zu groß; ein Akronym bleibt ein Wort:

import Antra::String

println(String.snake_case("getHTTPResponseCode"))   # get_http_response_code
println(String.camel_case("hello_world"))           # helloWorld
println(String.pascal_case("http-server"))          # HttpServer

Referenz#

import Antra::String as S

S.trim(str)
S.trim_start(str)
S.trim_end(str)
S.contains(str, needle)
S.starts_with(str, pre)
S.ends_with(str, suf)
S.split(str, pattern)
S.join(sep, list)
S.repeat(str, n)
S.words(str)
S.lines(str)
S.pad_left(n, str)
S.pad_right(n, str)
S.pad_start(width, str)
S.pad_start(width, str, [fill: "0"])
S.fixed(x, decimals)   # 16.666, 2 -> "16.67" (wie %.2f, rundet halb weg von 0)
S.pad_end(width, str)
S.pad_end(width, str, [fill: "."])
S.reverse(str)
S.replace(str, from, to)
S.slice(str, idx, len)
S.is_empty(str)
S.first(str)
S.first(str).!
S.last(str)
S.last!(str)
S.drop_start(str, n)
S.drop_end(str, n)

Funktionen#

fn camel_case(str)

The words of str in camel case: the first word lower case, the others capitalized, joined without a separator (raylib: TextToCamel).

Example:

String.camel_case("hello_world")    # "helloWorld"
String.camel_case("HTTP server")    # "httpServer"
fn chars(str :- String) :- [String]

Returns a list of single-character strings.

Example:

String.chars("abc")  # => ["a", "b", "c"]
fn codepoint(str :- String) :- Number

Unicode codepoint of the first character.

Example:

String.codepoint("€")   # 8364
fn codepoints(str :- String) :- [Number]

Codepoints of all characters.

fn contains(str :- String, needle :- String) :- Bool

Returns true if str contains needle.

Example:

String.contains("foobar", "oba")  # => true
fn drop_end(str :- String, n :- Number) :- String

Drops the last n characters of str.

Example:

String.drop_end("hello", 2)  # => "hel"
fn drop_start(str :- String, n :- Number) :- String

Drops the first n characters of str.

Example:

String.drop_start("hello", 2)  # => "llo"
fn ends_with(str :- String, suffix :- String) :- Bool

Returns true if str ends with suffix.

Example:

String.ends_with("hello", "llo")  # => true
fn first(str :- String) :- Result[String, String]

Returns %Ok[char] for the first character, or %Err["empty string"].

Example:

String.first("hello")  # => %Ok["h"]
fn fixed(x :- Number, decimals :- Number) :- String
fn fixed(_x, decimals)

x as text with exactly decimals digits after the point, rounded half away from zero (C's "%.2f" is String.fixed(x, 2)).

Example:

String.fixed(16.666, 2)  # => "16.67"
String.fixed(3, 1)       # => "3.0"
String.fixed(-2.5, 0)    # => "-3"
fn from_codepoint(n :- Number) :- String

One-character string for a Unicode codepoint.

fn from_codepoints(list :- [Number]) :- String

String from a list of codepoints.

fn is_empty(str :- String) :- Bool

Returns true if str has zero characters.

Example:

String.is_empty("")   # => true
String.is_empty("x")  # => false
fn join(sep :- String, list :- [String]) :- String

Joins a list of strings with sep.

Example:

String.join(",", ["a", "b", "c"])  # => "a,b,c"
fn last(str :- String) :- Result[String, String]

Returns %Ok[char] for the last character, or %Err["empty string"].

Example:

String.last("hello")  # => %Ok["o"]
fn len(str :- String) :- Number

Returns the number of characters in a string.

Example:

String.len("hello")  # => 5
fn lines(str :- String) :- [String]

Splits str on newlines, preserving a trailing empty element.

Example:

String.lines("a\nb\nc")  # => ["a", "b", "c"]
fn lower(str :- String) :- String

Converts a string to lowercase.

Example:

String.lower("HELLO")  # => "hello"
fn pad_end(width, str)
fn pad_end(width, str, [fill: fill = " ", ..])

Pads str on the right until it reaches width characters.

Example:

String.pad_end(5, "42")                # => "42   "
String.pad_end(4, "7", [fill: "."])  # => "7..."
fn pad_left(n :- Number, str :- String) :- String

Pads str on the left with n spaces.

Example:

String.pad_left(3, "hi")  # => "   hi"
fn pad_right(n :- Number, str :- String) :- String

Pads str on the right with n spaces.

Example:

String.pad_right(3, "hi")  # => "hi   "
fn pad_start(width, str)
fn pad_start(width, str, [fill: fill = " ", ..])

Pads str on the left until it reaches width characters.

Example:

String.pad_start(5, "42")             # => "   42"
String.pad_start(2, "7", [fill: "0"])  # => "07"
fn pascal_case(str)

The words of str in Pascal case: every word capitalized, joined without a separator (raylib: TextToPascal).

Example:

String.pascal_case("hello_world")    # "HelloWorld"
String.pascal_case("http-server")    # "HttpServer"
fn repeat(str :- String, n :- Number) :- String

Returns str repeated n times.

Example:

String.repeat("ab", 3)  # => "ababab"
fn replace(str :- String, from :- String, to :- String) :- String

Replaces all occurrences of from with to.

Example:

String.replace("foofoofoo", "foo", "bar")  # => "barbarbar"
fn reverse(str :- String) :- String

Returns the string with its characters in reverse order.

Example:

String.reverse("hello")  # => "olleh"
fn slice(str :- String, idx :- Number, len :- Number) :- String

Returns a substring starting at idx (negative: from end) with length len.

Example:

String.slice("hello", 1, 3)   # => "ell"
String.slice("hello", -3, 3)  # => "llo"
fn snake_case(str)

The words of str in snake case: lower case, joined by _. Words are separated by anything but letters and digits and by case changes, an acronym stays one word (raylib: TextToSnake).

Example:

String.snake_case("helloWorld")     # "hello_world"
String.snake_case("HTTPServer v2")  # "http_server_v2"
fn split(str :- String, sep :- String | Regex) :- [String]

Splits str on sep and returns the list of fields — always a list, never a Result (splitting cannot fail).

sep is polymorphic:

  • a string splits on that literal substring (no escaping needed — split("a.b", ".") splits on the dot, not on every character);
  • a regex"..." value splits on that regex pattern.

The split is lossless: n occurrences of the separator always yield n + 1 fields, so empty fields are kept — leading, middle, AND trailing. join(sep, split(s, sep)) reconstructs the input.

String.split("a,b,c", ",")        # => ["a", "b", "c"]
String.split("a.b.c", ".")        # => ["a", "b", "c"]   (literal dot)
String.split("a,b,",  ",")        # => ["a", "b", ""]    (trailing empty kept)
String.split(",a",    ",")        # => ["", "a"]         (leading empty kept)
String.split("",      ",")        # => [""]              (empty input)
String.split("abc",   ",")        # => ["abc"]           (separator absent)
String.split("abc",   "")         # => ["a", "b", "c"]   (empty sep -> characters)
String.split("one  two", regex"\s+")  # => ["one", "two"]   (regex, raw \s)

An invalid regex fails when the regex"..." literal is built (or via Regex.compile), not here.

fn starts_with(str :- String, prefix :- String) :- Bool

Returns true if str starts with prefix.

Example:

String.starts_with("hello", "hel")  # => true
fn trim(str :- String) :- String

Removes leading and trailing whitespace.

Example:

String.trim("  hi  ")  # => "hi"
fn trim_end(str :- String) :- String

Removes trailing whitespace.

Example:

String.trim_end("  hi  ")  # => "  hi"
fn trim_start(str :- String) :- String

Removes leading whitespace.

Example:

String.trim_start("  hi  ")  # => "hi  "
fn upper(str :- String) :- String

Converts a string to uppercase.

Example:

String.upper("hello")  # => "HELLO"
fn words(str :- String) :- [String]

Splits str on whitespace runs, ignoring leading/trailing whitespace.

Example:

String.words("  foo bar  ")  # => ["foo", "bar"]

Quelle dieser Seite