KScript Dokumentation
DE

Sprache

Automatisch aus dem Englischen übersetzt; das englische Original ist maßgeblich.

String-Felder und -Methoden

Text. Ein String, der eine Zahl enthält, kann auch Zahlen-Methoden verwenden.

Text ist ein string: alles in Anführungszeichen. 'Single quotes' behalten genau das, was du schreibst, und "double quotes" füllen aus, was in { } steht, sodass "Hello { $name }" die Variable liest. Für Text über mehrere Zeilen schreibst du einen Textblock zwischen ```-Zeilen, der jeden Zeilenumbruch so beibehält, wie geschrieben.

Text-Methoden ändern den Text, auf dem sie aufgerufen werden, nie; jede liefert neuen Text (oder eine Zahl, eine Liste oder einen bool-Wert). Verkette sie also oder speichere das Ergebnis in einer Variable:

<let name="name" value="'  ada LOVELACE '" />
<let name="tidy" value="$name->trim()->title()" />
<echo value="$tidy" /> <!-- 'Ada Lovelace' because the spaces go and each word gets a capital -->
<echo value="$name" /> <!-- '  ada LOVELACE ' because the text itself is unchanged -->
$name = '  ada LOVELACE '
$tidy = $name->trim()->title()
echo $tidy   // 'Ada Lovelace' because the spaces go and each word gets a capital
echo $name   // '  ada LOVELACE ' because the text itself is unchanged

Positionen zählen ab 0, und eine negative Position zählt vom Ende her. Ein Zeichen wie ein Emoji zählt als zwei Stellen, und keine Methode schneidet eines in der Mitte durch. Vergleiche berücksichtigen Groß- und Kleinschreibung exakt, außer du übergibst das Flag ignore_case.

Text untersuchen: count · count_of · is_empty · is_blank · is_numeric · contains · starts_with · ends_with · index_of · matches

Teil davon herauslösen: first · last · slice · before · after · truncate · find_all

Aufräumen und verändern: trim · squish · strip · replace · upper · lower · capitalize · title · case · pad · repeat · reverse · wrap · mask · normalize_unicode

In eine Liste aufteilen: split · lines · blocks · words · chars · numbers · grid · shapes

Als anderen Wert lesen: to_number · to_int · to_bool · to_date, oder in ein anderes Format mit ->convert()

Sicher zum Versenden machen: escape · url_encode · url_decode · to_base64 · from_base64 · md5 · sha1 · sha256 · sha512 · hmac

Ein Feld ist eine einfache Angabe über den Wert, die mit einem Punkt und ohne Klammern gelesen wird: $value.name. Eine Methode nimmt Argumente entgegen oder führt eine Aktion aus und wird mit -> aufgerufen: $value->name(arguments), oder mit dem übergebenen Wert: $value |> name(arguments).

Aus KScript.Core

#string->after(text: string, last?: bool) → string

Der Text nach dem ersten Vorkommen des Textes (oder dem letzten, mit last), oder der ganze Text, wenn er nicht vorkommt: 'a.tar.gz'->after('.', last) ist gz.

->after(text, last?) liefert den Text nach dem ersten Vorkommen von text, oder nach dem letzten Vorkommen mit last. Ist text nicht vorhanden, erhältst du den ganzen Text zurück:

<let name="file" value="'archive.tar.gz'" />
<echo value="$file->after('.')" /> <!-- 'tar.gz' because it cuts at the first dot -->
<echo value="$file->after('.', last)" /> <!-- 'gz' because it cuts at the last dot -->
<echo value="'no-at-sign'->after('@')" /> <!-- 'no-at-sign' because there is no @ to cut at -->
$file = 'archive.tar.gz'
echo $file->after('.')          // 'tar.gz' because it cuts at the first dot
echo $file->after('.', last)    // 'gz' because it cuts at the last dot
echo 'no-at-sign'->after('@')   // 'no-at-sign' because there is no @ to cut at
ParameterNimmt
text erforderlichstring
last optionalein Schalter: last oder not last

Argumente können benannt werden: ->after(…, last: …).

Beispiel
<echo>{ 'joan@example.com'->after('@') }</echo>
echo 'joan@example.com'->after('@')

#string->before(text: string, last?: bool) → string

Der Text vor dem ersten Vorkommen des Textes (oder dem letzten, mit last), oder der ganze Text, wenn er nicht vorkommt: 'a.tar.gz'->before('.', last) ist a.tar.

->before(text, last?) liefert den Text vor dem ersten Vorkommen von text, oder vor dem letzten Vorkommen mit last. Ist text nicht vorhanden, erhältst du den ganzen Text zurück:

<let name="email" value="'joan@example.com'" />
<echo value="$email->before('@')" /> <!-- 'joan' -->
<echo value="'archive.tar.gz'->before('.', last)" /> <!-- 'archive.tar' because it cuts at the last dot -->
$email = 'joan@example.com'
echo $email->before('@')                 // 'joan'
echo 'archive.tar.gz'->before('.', last) // 'archive.tar' because it cuts at the last dot
ParameterNimmt
text erforderlichstring
last optionalein Schalter: last oder not last

Argumente können benannt werden: ->before(…, last: …).

Beispiel
<echo>{ 'joan@example.com'->before('@') }</echo>
echo 'joan@example.com'->before('@')

#string->blocks() → string[]

Die Teile des Textes zwischen Leerzeilen, jeweils ohne die Zeilenumbrüche am Rand: 'a\nb\n\nc'->blocks() ist ['a\nb', 'c'].

->blocks() teilt den Text an Leerzeilen, sodass jeder Block aus einer Gruppe von Zeilen besteht. Ein einzelner Zeilenumbruch bleibt innerhalb eines Blocks; nur eine leere Zeile beginnt einen neuen. Jeder Block wird am Rand von Zeilenumbrüchen befreit:

<let name="pantry">```text
    tea
    milk

    flour
    sugar
    eggs
    ```</let>

<let name="shelves" value="$pantry->blocks()" />
<echo value="$shelves->count()" /> <!-- 2 because one blank line splits the text in two -->
<echo value="$shelves->map(.lines()->count())" /> <!-- [2, 3] because the first block has 2 lines and the second 3 -->
$pantry = ```text
tea
milk

flour
sugar
eggs
```

$shelves = $pantry->blocks()
echo $shelves->count()                  // 2 because one blank line splits the text in two
echo $shelves->map(.lines()->count())   // [2, 3] because the first block has 2 lines and the second 3
Beispiel
<echo>{ ('a' + @char_of(10) + @char_of(10) + 'b')->blocks() }</echo>
echo ('a' + @char_of(10) + @char_of(10) + 'b')->blocks()

#string->capitalize() → string

Der Text mit großgeschriebenem ersten Buchstaben.

->capitalize() macht den ersten Buchstaben groß und lässt den Rest unverändert:

<echo value="'élan and style'->capitalize()" /> <!-- 'Élan and style' because only the first letter changes -->
<echo value="'iPhone'->capitalize()" /> <!-- 'IPhone' -->
echo 'élan and style'->capitalize()   // 'Élan and style' because only the first letter changes
echo 'iPhone'->capitalize()           // 'IPhone'
Beispiel
<echo>{ 'hello world'->capitalize() }</echo>
echo 'hello world'->capitalize()

#string->case(style: snake|kebab|camel|pascal) → string

Der Text als Bezeichner in einem Stil: 'User ID'->case(snake) ist user_id, kebab ergibt user-id, camel userId und pascal UserId.

->case(style) schreibt den Text als Namen in einem Stil um: snake (user_id), kebab (user-id), camel (userId) oder pascal (UserId). Dabei erkennt es Wörter an Leerzeichen, Trennzeichen und Wechseln der Groß-/Kleinschreibung, sodass du von jedem Stil in jeden anderen wechseln kannst:

<echo value="'Order Total'->case(snake)" /> <!-- 'order_total' -->
<echo value="'order_total'->case(camel)" /> <!-- 'orderTotal' -->
<echo value="'orderTotal'->case(kebab)" /> <!-- 'order-total' -->
<echo value="'order-total'->case(pascal)" /> <!-- 'OrderTotal' -->
echo 'Order Total'->case(snake)   // 'order_total'
echo 'order_total'->case(camel)   // 'orderTotal'
echo 'orderTotal'->case(kebab)    // 'order-total'
echo 'order-total'->case(pascal)  // 'OrderTotal'
ParameterNimmt
style requiredeines dieser Wörter: snake|kebab|camel|pascal
Beispiel
<echo>{ 'Order Total'->case(snake) }</echo>
echo 'Order Total'->case(snake)

#string->chars() → string[]

Die Zeichen als Liste.

->chars() liefert die Zeichen als Liste, je ein Text pro Zeichen. Ein Zeichen wie ein Emoji ist ein einzelnes Element, egal wie es gespeichert ist:

<echo value="'tea'->chars()" /> <!-- ['t', 'e', 'a'] -->
<echo value="'👍🏽x'->chars()->count()" /> <!-- 2 because the thumb and its skin tone are one character -->
echo 'tea'->chars()   // ['t', 'e', 'a']
echo '👍🏽x'->chars()->count()   // 2 because the thumb and its skin tone are one character
Beispiel
<echo>{ 'abc'->chars() }</echo>
echo 'abc'->chars()

#string->contains(text, ignore_case?: bool) → bool

Wahr, wenn der Text diesen Text enthält. Mit ignore_case: true werden Groß- und Kleinschreibung nicht unterschieden.

->contains(text, ignore_case?) ist wahr, wenn der Text text irgendwo enthält. Mit ignore_case werden Groß- und Kleinschreibung gleich behandelt:

<echo value="'Green tea'->contains('tea')" /> <!-- true -->
<echo value="'Green tea'->contains('TEA')" /> <!-- false because case matters -->
<echo value="'Green tea'->contains('TEA', ignore_case)" /> <!-- true -->
echo 'Green tea'->contains('tea')                // true
echo 'Green tea'->contains('TEA')                // false because case matters
echo 'Green tea'->contains('TEA', ignore_case)   // true
ParameterNimmt
text erforderlichein beliebiger Wert
ignore_case optionalein Schalter: ignore_case oder not ignore_case

Argumente können benannt werden: ->contains(…, ignore_case: …).

Beispiel
<echo>{ 'hello world'->contains('world') }</echo>
echo 'hello world'->contains('world')

#string->count() → number

Die Anzahl der Zeichen.

->count() liefert, wie viele Zeichen der Text hat:

<echo value="'tea'->count()" /> <!-- 3 -->
<echo value="''->count()" /> <!-- 0 -->
echo 'tea'->count()   // 3
echo ''->count()      // 0
Beispiel
<echo>{ 'hello'->count() }</echo>
echo 'hello'->count()

#string->count_of(match) → number

Wie oft ein Textabschnitt vorkommt.

->count_of(match) zählt, wie oft ein Textstück vorkommt. Vorkommen überlappen sich nicht:

<echo value="'banana'->count_of('an')" /> <!-- 2 because b-an-an-a has two -->
<echo value="'banana'->count_of('x')" /> <!-- 0 -->
echo 'banana'->count_of('an')   // 2 because b-an-an-a has two
echo 'banana'->count_of('x')    // 0
Beispiel
<echo>{ 'banana'->count_of('a') }</echo>
echo 'banana'->count_of('a')

#string->ends_with(text, ignore_case?: bool) → bool

True, wenn der Text mit diesem Text endet. Mit ignore_case: true werden Groß- und Kleinschreibung ignoriert.

->ends_with(text, ignore_case?) ist wahr, wenn der Text mit text endet:

<echo value="'photo.JPG'->ends_with('.jpg')" /> <!-- false because case matters -->
<echo value="'photo.JPG'->ends_with('.jpg', ignore_case)" /> <!-- true -->
echo 'photo.JPG'->ends_with('.jpg')                // false because case matters
echo 'photo.JPG'->ends_with('.jpg', ignore_case)   // true
ParameterNimmt
text erforderlichein beliebiger Wert
ignore_case optionalein Schalter: ignore_case oder not ignore_case

Argumente können benannt werden: ->ends_with(…, ignore_case: …).

Beispiel
<echo>{ 'notes.txt'->ends_with('.txt') }</echo>
echo 'notes.txt'->ends_with('.txt')

#string->escape() → string

Der Text, HTML-escaped.

->escape() macht Text sicher für HTML: <, >, & und Anführungszeichen werden zu Entities, sodass etwas, das ein Nutzer eingegeben hat, als Text angezeigt wird und nicht zu Markup wird:

<echo value="'&lt;b>Tea &amp; milk&lt;/b>'->escape()" /> <!-- '&lt;b&gt;Tea &amp; milk&lt;/b&gt;' -->
echo '<b>Tea & milk</b>'->escape()   // '&lt;b&gt;Tea &amp; milk&lt;/b&gt;'

Seiten maskieren bereits, was sie ausgeben; dies ist für HTML gedacht, das du selbst erstellst.

Beispiel
<echo>{ 'Say "hi" > 3'->escape() }</echo>
echo 'Say "hi" > 3'->escape()

#string->find_all(pattern: regex) → string[]

Jeder Treffer eines regulären Ausdrucks als Liste.

->find_all(pattern) liefert jede Übereinstimmung eines regulären Ausdrucks, in der Reihenfolge:

<echo value="'tea 12, milk 7'->find_all(/\d+/)" /> <!-- ['12', '7'] because those are the runs of digits -->
echo 'tea 12, milk 7'->find_all(/\d+/)   // ['12', '7'] because those are the runs of digits

->numbers() liefert die Zahlen selbst, als Zahlen.

ParameterNimmt
pattern erforderlichregex
Beispiel
<echo>{ 'Call 555-1234 or 555-9876'->find_all('[0-9]{3}-[0-9]{4}') }</echo>
echo 'Call 555-1234 or 555-9876'->find_all('[0-9]{3}-[0-9]{4}')

#string->first() → string

Das erste Zeichen.

->first() liefert das erste Zeichen, oder '' bei leerem Text:

<echo value="'tea'->first()" /> <!-- 't' -->
<echo value="''->first()" /> <!-- '' -->
echo 'tea'->first()   // 't'
echo ''->first()      // ''
Beispiel
<echo>{ 'Joan'->first() }</echo>
echo 'Joan'->first()

#string->from_base64() → string

Base64-Text, dekodiert.

->from_base64() dekodiert Base64-Text zurück in den Text, aus dem er erstellt wurde. Text, der kein Base64 ist, führt zu einem Fehler:

<echo value="'dGVh'->from_base64()" /> <!-- 'tea' -->
echo 'dGVh'->from_base64()   // 'tea'
Beispiel
<echo>{ 'aGVsbG8='->from_base64() }</echo>
echo 'aGVsbG8='->from_base64()

#string->grid() → string[][]

Der Text als Zeilen aus Zeichen, je eine Liste pro Zeile, für Karten und Labyrinthe: '#.\n.#'->grid() ist [['#', '.'], ['.', '#']]. Ein Zeilenumbruch am Ende fügt keine leere Zeile hinzu. Eine Zelle liest du mit $grid[row][column], und du durchläufst ihre Zeilen oder Spalten mit ->cells(rows) bzw. ->cells(columns).

->grid() wandelt Zeilen aus Zeichen in Reihen einzelner Zeichen um, sodass $grid[row][column] ein einzelnes Zeichen ist. Zeile 0 ist die oberste Zeile und Spalte 0 das erste Zeichen. Ein Zeilenumbruch am Ende fügt keine leere Zeile hinzu:

<let name="garden">```text
    #.#
    ..#
    ```</let>

<let name="plot" value="$garden->grid()" />
<echo value="$plot" /> <!-- [['#', '.', '#'], ['.', '.', '#']] -->
<echo value="$plot[0][1]" /> <!-- '.' because row 0 is #.# and column 1 is its middle -->
<echo value="$plot[1][2]" /> <!-- '#' because row 1 is ..# and column 2 is its last -->
$garden = ```text
#.#
..#
```

$plot = $garden->grid()
echo $plot          // [['#', '.', '#'], ['.', '.', '#']]
echo $plot[0][1]    // '.' because row 0 is #.# and column 1 is its middle
echo $plot[1][2]    // '#' because row 1 is ..# and column 2 is its last

Um die Zeilen oder Spalten zu durchlaufen, liest du das Raster mit ->cells(rows) oder ->cells(columns).

Beispiel
<echo>{ ('#.' + @char_of(10) + '.#')->grid() }</echo>
echo ('#.' + @char_of(10) + '.#')->grid()

#string->hmac(key, algorithm?: sha256|sha1|sha512) → string

Die HMAC des Textes mit einem geheimen Schlüssel, hexadezimal (standardmäßig SHA-256), so wie Webhook-Signaturen erstellt werden. Vergleiche Signaturen mit @secure_equals().

->hmac(key, algorithm?) signiert den Text mit einem geheimen Schlüssel und liefert die Signatur hexadezimal, SHA-256 sofern du nicht sha1 oder sha512 übergibst. So beweisen Webhooks, woher eine Nachricht stammt. Vergleiche Signaturen mit @secure_equals(), das unabhängig davon, wie viel übereinstimmt, gleich lange braucht:

<let name="body" value="'{&quot;id&quot;: 1}'" />
<let name="signature" value="$body->hmac('whsec_test')" />
<echo value="$signature->count()" /> <!-- 64 because SHA-256 in hex is 64 characters -->
<echo value="@secure_equals($signature, $body->hmac('whsec_test'))" /> <!-- true because the same key signs the same text the same way -->
$body = '{"id": 1}'
$signature = $body->hmac('whsec_test')
echo $signature->count()                                    // 64 because SHA-256 in hex is 64 characters
echo @secure_equals($signature, $body->hmac('whsec_test'))  // true because the same key signs the same text the same way
ParameterNimmt
key erforderlichein beliebiger Wert
algorithm optionaleines dieser Wörter: sha256|sha1|sha512

Argumente können benannt werden: ->hmac(…, algorithm: …).

Beispiel
<echo>{ '{"id": 1}'->hmac('whsec_test') }</echo>
echo '{"id": 1}'->hmac('whsec_test')

#string->index_of(text, ignore_case?: bool, last?: bool) → number

Wo der Text zuerst vorkommt, oder mit last, wo er zuletzt vorkommt; -1, wenn er nicht vorkommt. Mit ignore_case werden Groß- und Kleinschreibung gleich behandelt.

->index_of(text, ignore_case?, last?) liefert die Position, an der text zuerst vorkommt, oder mit last die, an der es zuletzt vorkommt. Ist -1 nicht vorhanden, ergibt sich text:

<echo value="'banana'->index_of('an')" /> <!-- 1 because positions start at 0 -->
<echo value="'banana'->index_of('an', last)" /> <!-- 3 -->
<echo value="'banana'->index_of('x')" /> <!-- -1 -->
echo 'banana'->index_of('an')         // 1 because positions start at 0
echo 'banana'->index_of('an', last)   // 3
echo 'banana'->index_of('x')          // -1
ParameterNimmt
text erforderlichein beliebiger Wert
ignore_case optionalein Schalter: ignore_case oder not ignore_case
last optionalein Schalter: last oder not last

Argumente können benannt werden: ->index_of(…, ignore_case: …, last: …).

Beispiel
<echo>{ 'hello'->index_of('l') }</echo>
echo 'hello'->index_of('l')

#string->is_blank() → bool

Wahr, wenn der Text leer ist oder nur aus Leerzeichen besteht.

->is_blank() ist wahr, wenn der Text leer ist oder nur aus Leerzeichen, Tabulatoren und Zeilenumbrüchen besteht. Verwende es, um zu prüfen, was jemand in ein Formular eingegeben hat:

<echo value="''->is_blank()" /> <!-- true -->
<echo value="'   '->is_blank()" /> <!-- true because spaces alone count as blank -->
<echo value="' tea '->is_blank()" /> <!-- false -->
echo ''->is_blank()       // true
echo '   '->is_blank()    // true because spaces alone count as blank
echo ' tea '->is_blank()  // false
Beispiel
<echo>{ '   '->is_blank() }</echo>
echo '   '->is_blank()

#string->is_empty() → bool

Wahr, wenn der Text null Zeichen hat.

->is_empty() ist wahr, wenn der Text überhaupt keine Zeichen hat. Leerzeichen zählen als Zeichen, daher ist ' ' nicht leer; verwende dafür ->is_blank():

<echo value="''->is_empty()" /> <!-- true -->
<echo value="'   '->is_empty()" /> <!-- false because it has three spaces -->
echo ''->is_empty()      // true
echo '   '->is_empty()   // false because it has three spaces
Beispiel
<echo>{ ''->is_empty() }</echo>
echo ''->is_empty()

#string->is_numeric() → bool

Wahr, wenn der gesamte Text eine Zahl ist, etwa '42', '-3.5' oder '1e3'. Leerzeichen davor und danach sind erlaubt.

->is_numeric() ist wahr, wenn der gesamte Text eine Zahl ist, etwa '42', '-3.5' oder '1e3'. Leerzeichen davor und danach sind erlaubt:

<echo value="'42'->is_numeric()" /> <!-- true -->
<echo value="' -3.5 '->is_numeric()" /> <!-- true because the spaces around it are fine -->
<echo value="'42 cats'->is_numeric()" /> <!-- false because cats isn't part of a number -->
echo '42'->is_numeric()      // true
echo ' -3.5 '->is_numeric()  // true because the spaces around it are fine
echo '42 cats'->is_numeric() // false because cats isn't part of a number
Beispiel
<echo>{ ['42', '-3.5', 'abc']->map(fn(t) => $t->is_numeric()) }</echo>
echo ['42', '-3.5', 'abc']->map(fn(t) => $t->is_numeric())

#string->last() → string

Das letzte Zeichen.

->last() gibt das letzte Zeichen zurück oder '' bei leerem Text:

<echo value="'tea'->last()" /> <!-- 'a' -->
echo 'tea'->last()   // 'a'
Beispiel
<echo>{ 'Joan'->last() }</echo>
echo 'Joan'->last()

#string->lines() → string[]

Die Zeilen des Textes als Liste.

->lines() gibt eine Textzeile pro Zeile zurück. Eine leere Zeile ergibt einen leeren Text, sodass du die Trennungen zwischen Gruppen finden kannst:

<let name="note">```text
    tea
    milk

    juice
    ```</let>

<echo value="$note->lines()" /> <!-- ['tea', 'milk', '', 'juice'] -->
<echo value="$note->lines()->count()" /> <!-- 4 because the blank line is a line too -->
$note = ```text
tea
milk

juice
```

echo $note->lines()          // ['tea', 'milk', '', 'juice']
echo $note->lines()->count() // 4 because the blank line is a line too

->blocks() teilt stattdessen an den Leerzeilen.

Beispiel
<echo>{ ('one' + @char_of(10) + 'two')->lines() }</echo>
echo ('one' + @char_of(10) + 'two')->lines()

#string->lower() → string

Der Text in Kleinbuchstaben.

->lower() gibt den Text in Kleinbuchstaben zurück:

<echo value="'Green TEA'->lower()" /> <!-- 'green tea' -->
echo 'Green TEA'->lower()   // 'green tea'
Beispiel
<echo>{ 'HELLO'->lower() }</echo>
echo 'HELLO'->lower()

#string->mask(char?: string, keep_end?: number) → string

Der Text, in dem jedes Zeichen durch char ersetzt wird (standardmäßig *), außer den letzten keep_end Zeichen: ->mask('*', keep_end: 4) zeigt nur die letzten vier.

->mask(char?, keep_end?) verbirgt den Text hinter einem Zeichen, *, sofern du kein anderes angibst, und behält die letzten keep_end Zeichen, damit der Leser ihn wiedererkennen kann:

<echo value="'4242424242424242'->mask('*', keep_end: 4)" /> <!-- '************4242' because only the last 4 show -->
<echo value="'secret'->mask()" /> <!-- '******' -->
echo '4242424242424242'->mask('*', keep_end: 4)   // '************4242' because only the last 4 show
echo 'secret'->mask()                             // '******'
ParameterNimmt
char optionalstring
keep_end optionalnumber

Argumente können benannt werden: ->mask(…, keep_end: …).

Beispiel
<echo>{ '4242424242424242'->mask('*', keep_end: 4) }</echo>
echo '4242424242424242'->mask('*', keep_end: 4)

#string->matches(pattern: regex) → bool

Wahr, wenn der Text einem regulären Ausdruck entspricht: ->matches(/^[a-z]+$/i).

->matches(pattern) ist wahr, wenn ein regulärer Ausdruck im Text einen Treffer findet. Verankere ihn mit ^ und $, um den gesamten Text zu prüfen:

<echo value="'Tea'->matches(/^[a-z]+$/i)" /> <!-- true because i ignores case -->
<echo value="'Tea2'->matches(/^[a-z]+$/i)" /> <!-- false because 2 isn't a letter -->
echo 'Tea'->matches(/^[a-z]+$/i)   // true because i ignores case
echo 'Tea2'->matches(/^[a-z]+$/i)  // false because 2 isn't a letter
ParameterNimmt
pattern erforderlichregex
Beispiel
<echo>{ '2026-11-19'->matches('^[0-9]{4}-[0-9]{2}-[0-9]{2}$') }</echo>
echo '2026-11-19'->matches('^[0-9]{4}-[0-9]{2}-[0-9]{2}$')

#string->md5() → string

Der MD5-Hash des Textes, hexadezimal.

->md5() gibt den MD5-Hash des Textes hexadezimal zurück. Er ist schnell und für einen Cache-Schlüssel oder eine Prüfsumme geeignet, aber nicht für Passwörter oder Signaturen:

<echo value="'tea'->md5()" /> <!-- '7239ea2b5dc943f61f3c0a0276c20974' -->
<echo value="'tea'->md5()->count()" /> <!-- 32 because MD5 in hex is 32 characters -->
echo 'tea'->md5()           // '7239ea2b5dc943f61f3c0a0276c20974'
echo 'tea'->md5()->count()  // 32 because MD5 in hex is 32 characters
Beispiel
<echo>{ 'hello'->md5() }</echo>
echo 'hello'->md5()

#string->normalize_unicode(form?: NFC|NFD|NFKC|NFKD) → string

Normalisiert Unicode in der angegebenen Form (standardmäßig NFC).

->normalize_unicode(form?) sorgt dafür, dass optisch gleiche Texte auch gleich verglichen werden. é kann als ein Zeichen oder als e plus Akzent gespeichert sein; NFC (der Standard) fügt sie zusammen, NFD trennt sie:

<echo value="'café'->normalize_unicode(NFD)->count()" /> <!-- 5 because NFD stores é as e and an accent -->
<echo value="'café'->normalize_unicode()->count()" /> <!-- 4 -->
echo 'café'->normalize_unicode(NFD)->count()   // 5 because NFD stores é as e and an accent
echo 'café'->normalize_unicode()->count()      // 4
ParameterNimmt
form optionaleines dieser Wörter: NFC|NFD|NFKC|NFKD
Beispiel
<echo>{ ('e' + @char_of(769))->normalize_unicode() }</echo>
echo ('e' + @char_of(769))->normalize_unicode()

#string->numbers() → number[]

Jede Zahl im Text, der Reihe nach, als Zahlen: 'x=-3, y=12'->numbers() ist [-3, 12]. Ein Minus macht eine Zahl nur dann negativ, wenn direkt davor kein Buchstabe oder keine Ziffer steht; daher ist '1-3'->numbers() [1, 3].

->numbers() gibt jede im Text geschriebene Zahl der Reihe nach als Zahl zurück. Ein Minus zählt als Vorzeichen, wenn links nichts angehängt ist, wie in x=-3; zwischen zwei Zahlen, wie in 1-3, ist es ein Bindestrich:

<echo value="'move 3 boxes from -1 to 12'->numbers()" /> <!-- [3, -1, 12] because -1 has a space before its minus -->
<echo value="'pages 4-9'->numbers()" /> <!-- [4, 9] because the minus joins 4 and 9, so it is a dash -->
<echo value="'price 2.50, qty 4'->numbers()" /> <!-- [2.5, 4] -->
echo 'move 3 boxes from -1 to 12'->numbers()  // [3, -1, 12] because -1 has a space before its minus
echo 'pages 4-9'->numbers()                   // [4, 9] because the minus joins 4 and 9, so it is a dash
echo 'price 2.50, qty 4'->numbers()           // [2.5, 4]
Beispiel
<echo>{ 'x=-3, y=12'->numbers() }</echo>
echo 'x=-3, y=12'->numbers()

#string->pad(width: number, char?, start?: bool) → string

Der Text, am Ende auf width aufgefüllt, oder am Anfang mit start: '7'->pad(3, '0', start) ergibt 007.

->pad(width, char?, start?) fügt ein Zeichen hinzu, standardmäßig ein Leerzeichen, bis der Text width Zeichen lang ist. Es wird am Ende aufgefüllt oder mit start am Anfang. Text, der bereits so lang ist, bleibt unverändert:

<echo value="'7'->pad(3, '0', start)" /> <!-- '007' because two zeros go in front -->
<echo value="'tea'->pad(5, '.')" /> <!-- 'tea..' -->
<echo value="'teapot'->pad(3)" /> <!-- 'teapot' because it is already longer than 3 -->
echo '7'->pad(3, '0', start)   // '007' because two zeros go in front
echo 'tea'->pad(5, '.')        // 'tea..'
echo 'teapot'->pad(3)          // 'teapot' because it is already longer than 3
ParameterNimmt
width erforderlichnumber
char optionalein beliebiger Wert
start optionalein Schalter: start oder not start

Argumente können benannt werden: ->pad(…, char: …, start: …).

Beispiel
<echo>{ '7'->pad(3, '0', start) }</echo>
echo '7'->pad(3, '0', start)

#string->repeat(count: number) → string

Der Text, count-mal wiederholt.

->repeat(count) gibt den Text count-mal wiederholt zurück:

<echo value="'ha'->repeat(3)" /> <!-- 'hahaha' -->
<echo value="'-'->repeat(10)" /> <!-- '- -- -- -- -- -' -->
echo 'ha'->repeat(3)    // 'hahaha'
echo '-'->repeat(10)    // '----------'
ParameterNimmt
count erforderlichnumber
Beispiel
<echo>{ 'ab'->repeat(3) }</echo>
echo 'ab'->repeat(3)

#string->replace(old, new, ignore_case?: bool) → string

The text with every old replaced by new. old can be a regex: ->replace(/\s+/, ' '), and new can use its groups as $1 or $. With ignore_case, plain text matches in any case.

->replace(old, new, ignore_case?) ersetzt jedes old durch new. old kann ein regulärer Ausdruck sein, und dann kann new die Treffer seiner Gruppen als $1 oder ${name} verwenden:

<echo value="'banana'->replace('a', 'o')" /> <!-- 'bonono' because every a changes -->
<echo value="'Tea'->replace('t', 'p', ignore_case)" /> <!-- 'pea' because T matches t -->
<echo value="'too   many  spaces'->replace(/\s+/, ' ')" /> <!-- 'too many spaces' -->
<echo value="'2026-12-01'->replace(/(\d+)-(\d+)-(\d+)/, '$3/$2/$1')" /> <!-- '01/12/2026' because the groups swap places -->
echo 'banana'->replace('a', 'o')                   // 'bonono' because every a changes
echo 'Tea'->replace('t', 'p', ignore_case)         // 'pea' because T matches t
echo 'too   many  spaces'->replace(/\s+/, ' ')    // 'too many spaces'
echo '2026-12-01'->replace(/(\d+)-(\d+)-(\d+)/, '$3/$2/$1')   // '01/12/2026' because the groups swap places
ParameterNimmt
old erforderlichein beliebiger Wert
new erforderlichein beliebiger Wert
ignore_case optionalein Schalter: ignore_case oder not ignore_case

Argumente können benannt werden: ->replace(…, new: …, ignore_case: …).

Beispiel
<echo>{ 'Hello, World'->replace(old: 'World', new: 'Joan') }</echo>
echo 'Hello, World'->replace(old: 'World', new: 'Joan')

#string->reverse() → string

Der Text rückwärts.

->reverse() gibt die Zeichen in umgekehrter Reihenfolge zurück, wobei jedes Zeichen ganz bleibt:

<echo value="'stressed'->reverse()" /> <!-- 'desserts' -->
echo 'stressed'->reverse()   // 'desserts'
Beispiel
<echo>{ 'stressed'->reverse() }</echo>
echo 'stressed'->reverse()

#string->sha1() → string

Der SHA-1-Hash des Textes, hexadezimal. Nur dort verwenden, wo ein anderes System SHA-1 verlangt.

->sha1() gibt den SHA-1-Hash des Textes hexadezimal zurück, 40 Zeichen. Verwende für Neues bevorzugt ->sha256():

<echo value="'tea'->sha1()->count()" /> <!-- 40 -->
echo 'tea'->sha1()->count()   // 40
Beispiel
<echo>{ 'hello'->sha1() }</echo>
echo 'hello'->sha1()

#string->sha256() → string

Der SHA-256-Hash des Textes, hexadezimal.

->sha256() gibt den SHA-256-Hash des Textes hexadezimal zurück, 64 Zeichen. Derselbe Text ergibt immer denselben Hash, und jede Änderung ergibt einen anderen:

<echo value="'tea'->sha256()->count()" /> <!-- 64 -->
<echo value="'tea'->sha256() == 'tea'->sha256()" /> <!-- true because the same text gives the same hash -->
<echo value="'tea'->sha256() == 'Tea'->sha256()" /> <!-- false because one letter changed -->
echo 'tea'->sha256()->count()                  // 64
echo 'tea'->sha256() == 'tea'->sha256()        // true because the same text gives the same hash
echo 'tea'->sha256() == 'Tea'->sha256()        // false because one letter changed
Beispiel
<echo>{ 'hello'->sha256() }</echo>
echo 'hello'->sha256()

#string->sha512() → string

Der SHA-512-Hash des Textes, hexadezimal.

->sha512() gibt den SHA-512-Hash des Textes hexadezimal zurück, 128 Zeichen:

<echo value="'tea'->sha512()->count()" /> <!-- 128 -->
echo 'tea'->sha512()->count()   // 128
Beispiel
<echo>{ 'hello'->sha512()->slice(0, 16) }</echo>
echo 'hello'->sha512()->slice(0, 16)

#string->shapes(mask?: any, square?: bool, rectangle?: bool, line?: bool, circle?: bool, of?: any, rotated?: bool, size?: number, radius?: number, distance?: straight|steps|moves) → list

Every place a mask fits (->shapes(Tree), or ->shapes([Block, Ell]) for several), and every square, rectangle, line or circle (name one or several: ->shapes(square, circle, of: '#')) made by the cells that hold of: (a value, or a regex). It looks through text (each line a row, each character a cell, a blank line between layers), a list of values (one row), a grid ($grid[row][column]) or a 3D list ($cube[layer][row][column]); the built-in shapes also take a list of points { row, column }. A mask is a type that extends Mask: its layout is a picture, one character a cell, and each character matches a cell holding the same character, unless its symbols say otherwise (a value, a regex, a fn(cell), or any for a cell that isn't part of the shape). Each match is { kind, points }: kind is the mask's name or the shape, and each point is { row, column, value } (with symbol for a mask, and layer in 3D). With rotated, a mask is tried turned every way the input allows (back to front in a row, the four quarter turns in a grid, all 24 ways round in 3D), and squares, rectangles and lines may lean. A line needs size:, and a circle radius: and distance: straight (the default), steps or moves.

->shapes(mask?, square?, rectangle?, line?, circle?, of?, rotated?, size?, radius?, distance?) finds shapes drawn in text. Each line is a row and each character a cell, so the text doesn't need ->grid() first, and a blank line starts the next layer of a 3D input. It works as the list method does: name a mask first (a type that extends Mask, or a list of them), or one of the built-in shapes with of: to say which cells make it. Each match comes back as { kind, points }, and each point says its row, column, value and the mask's symbol drawn over it. See list ->shapes() for every argument.

<let name="orchard">```text
    #.#.#
    .#...
    #.#.#
    ```</let>

<type name="Tree" extends="Mask">
  <field name="layout" default="```text
      #.#
      .#.
      ```" />
  <field name="symbols" default="{ '.': any }" />
</type>

<echo value="$orchard->shapes(Tree)->count()" /> <!-- 1 because the V of three trees fits only at the top left -->
<echo value="$orchard->shapes(Tree, rotated)->count()" /> <!-- 4 because turned upside down and on its sides it fits three more places -->
<echo value="$orchard->shapes(Tree)->first().points->map(.column)" /> <!-- [0, 2, 1] because the points come in reading order: the top two, then the middle -->
$orchard = ```text
#.#.#
.#...
#.#.#
```

type Tree extends Mask {
  layout: ```text
    #.#
    .#.
    ```
  symbols: { '.': any }
}

echo $orchard->shapes(Tree)->count()                      // 1 because the V of three trees fits only at the top left
echo $orchard->shapes(Tree, rotated)->count()             // 4 because turned upside down and on its sides it fits three more places
echo $orchard->shapes(Tree)->first().points->map(.column) // [0, 2, 1] because the points come in reading order: the top two, then the middle
ParameterNimmt
mask optionalany
square optionalein Flag: square oder not square
rectangle optionaleine Option: rectangle oder not rectangle
line optionaleine Option: line oder not line
circle optionaleine Option: circle oder not circle
of optionalany
rotated optionaleine Option: rotated oder not rotated
size optionalnumber
radius optionalnumber
distance optionaleines dieser Wörter: straight|steps|moves

Argumente können benannt werden: ->shapes(…, square: …, rectangle: …).

Beispiel
<echo>{ ('##' + @char_of(10) + '##')->shapes(square, of: '#')->count() }</echo>
echo ('##' + @char_of(10) + '##')->shapes(square, of: '#')->count()

#string->slice(start: number, end?: number) → string

Teil des Textes; negative Positionen zählen vom Ende.

->slice(start, end?) gibt die Zeichen ab Position start bis, aber ohne, end zurück, oder bis zum Textende. Negative Positionen zählen vom Ende:

<let name="word" value="'teapot'" />
<echo value="$word->slice(0, 3)" /> <!-- 'tea' because it stops before position 3 -->
<echo value="$word->slice(3)" /> <!-- 'pot' -->
<echo value="$word->slice(-3)" /> <!-- 'pot' because -3 is three from the end -->
$word = 'teapot'
echo $word->slice(0, 3)   // 'tea' because it stops before position 3
echo $word->slice(3)      // 'pot'
echo $word->slice(-3)     // 'pot' because -3 is three from the end
ParameterNimmt
start erforderlichnumber
end optionalnumber

Argumente können benannt werden: ->slice(…, end: …).

Beispiel
<echo>{ 'KScript'->slice(1, 4) }</echo>
echo 'KScript'->slice(1, 4)

#string->split(separator?) → string[]

Die Teile zwischen jedem Trenner (oder zwischen Leerzeichen) als Liste. Der Trenner kann ein Regex sein: ->split(/\s*,\s*/).

->split(separator?) schneidet den Text an jedem Trenner und gibt die Teile als Liste zurück. Ohne Trenner wird an Leerzeichen geschnitten. Der Trenner kann ein regulärer Ausdruck sein:

<echo value="'tea,milk,juice'->split(',')" /> <!-- ['tea', 'milk', 'juice'] -->
<echo value="'tea milk'->split()" /> <!-- ['tea', 'milk'] -->
<echo value="'a, b,c'->split(/\s*,\s*/)" /> <!-- ['a', 'b', 'c'] because the spaces around each comma go too -->
echo 'tea,milk,juice'->split(',')       // ['tea', 'milk', 'juice']
echo 'tea milk'->split()                // ['tea', 'milk']
echo 'a, b,c'->split(/\s*,\s*/)         // ['a', 'b', 'c'] because the spaces around each comma go too
Beispiel
<echo>{ 'a,b,c'->split(',') }</echo>
echo 'a,b,c'->split(',')

#string->squish() → string

Der Text, an den Enden getrimmt, wobei jede Folge von Leerzeichen, Tabs und Zeilenumbrüchen darin zu einem Leerzeichen wird.

->squish() entfernt Leerzeichen am Anfang und Ende des Textes und macht jede Folge von Leerzeichen, Tabulatoren und Zeilenumbrüchen darin zu einem einzigen Leerzeichen:

<echo value="'  too   many    spaces '->squish()" /> <!-- 'too many spaces' -->
echo '  too   many    spaces '->squish()   // 'too many spaces'
Beispiel
<echo>{ '  too   many    spaces '->squish() }</echo>
echo '  too   many    spaces '->squish()

#string->starts_with(text, ignore_case?: bool) → bool

Wahr, wenn der Text mit diesem Text beginnt. Mit ignore_case: true werden Groß- und Kleinschreibung ignoriert.

->starts_with(text, ignore_case?) ist wahr, wenn der Text mit text beginnt:

<echo value="'https://example.com'->starts_with('https://')" /> <!-- true -->
<echo value="'HTTP://x'->starts_with('http', ignore_case)" /> <!-- true because case is ignored -->
echo 'https://example.com'->starts_with('https://')   // true
echo 'HTTP://x'->starts_with('http', ignore_case)     // true because case is ignored
ParameterNimmt
text erforderlichein beliebiger Wert
ignore_case optionalein Schalter: ignore_case oder not ignore_case

Argumente können benannt werden: ->starts_with(…, ignore_case: …).

Beispiel
<echo>{ 'kscript.json'->starts_with('kscript') }</echo>
echo 'kscript.json'->starts_with('kscript')

#string->strip(prefix?: string, suffix?: string) → string

Der Text ohne genau ein Präfix, Suffix oder beides, sofern vorhanden: 'v1.2'->strip(prefix: 'v'), 'a.txt'->strip(suffix: '.txt').

->strip(prefix?, suffix?) entfernt genau ein Präfix, Suffix oder beides, sofern vorhanden. Anders als ->trim() entfernt es das ganze Stück einmal, nicht jedes Zeichen:

<echo value="'v1.2'->strip(prefix: 'v')" /> <!-- '1.2' -->
<echo value="'notes.txt'->strip(suffix: '.txt')" /> <!-- 'notes' -->
<echo value="'[tea]'->strip(prefix: '[', suffix: ']')" /> <!-- 'tea' -->
<echo value="'tea'->strip(prefix: 'x')" /> <!-- 'tea' because there is no x to take off -->
echo 'v1.2'->strip(prefix: 'v')                 // '1.2'
echo 'notes.txt'->strip(suffix: '.txt')         // 'notes'
echo '[tea]'->strip(prefix: '[', suffix: ']')   // 'tea'
echo 'tea'->strip(prefix: 'x')                  // 'tea' because there is no x to take off
ParameterNimmt
prefix optionalstring
suffix optionalstring

Argumente können benannt werden: ->strip(…, suffix: …).

Beispiel
<echo>{ 'draft-notes.md'->strip(prefix: 'draft-', suffix: '.md') }</echo>
echo 'draft-notes.md'->strip(prefix: 'draft-', suffix: '.md')

#string->title() → string

Der Text in Title Case.

->title() gibt den Text in Title Case aus: der erste Buchstabe jedes Wortes in Großbuchstaben:

<echo value="'the tea party'->title()" /> <!-- 'The Tea Party' -->
echo 'the tea party'->title()   // 'The Tea Party'
Beispiel
<echo>{ 'the quick brown fox'->title() }</echo>
echo 'the quick brown fox'->title()

#string->to_base64(url?: bool) → base64

Der Text als Base64 kodiert. Mit url: true ist es URL-sicheres Base64 ohne Auffüllung.

->to_base64(url?) kodiert Text als Base64, das nur Buchstaben, Ziffern, +, / und = verwendet, sodass er dort übertragen werden kann, wo andere Zeichen nicht funktionieren, etwa in einem HTTP-Header. Mit url verwendet es stattdessen - und _ und lässt das =-Auffüllen weg, sodass es in eine URL passt. Das Ergebnis ist ein base64-Wert, dessen ->decode() den Text wiederherstellt:

<echo value="'tea'->to_base64()" /> <!-- 'dGVh' -->
<echo value="'tea?'->to_base64()" /> <!-- 'dGVhPw==' -->
<echo value="'tea?'->to_base64(url)" /> <!-- 'dGVhPw' because url leaves off the padding -->
<echo value="'tea'->to_base64()->decode()" /> <!-- 'tea' because the base64 value decodes back to the text -->
echo 'tea'->to_base64()                 // 'dGVh'
echo 'tea?'->to_base64()                // 'dGVhPw=='
echo 'tea?'->to_base64(url)             // 'dGVhPw' because url leaves off the padding
echo 'tea'->to_base64()->decode()       // 'tea' because the base64 value decodes back to the text
ParameterNimmt
url optionalein Flag: url oder not url
Beispiel
<echo>{ 'hello'->to_base64() }</echo>
echo 'hello'->to_base64()

#string->to_bool() → bool

Text wie 'true', 'false', 'yes', 'no', '1' oder '0' als bool.

->to_bool() liest Text wie 'true', 'false', 'yes', 'no', '1' oder '0' als bool. Anderer Text ist ein Typfehler:

<echo value="'yes'->to_bool()" /> <!-- true -->
<echo value="'0'->to_bool()" /> <!-- false -->
echo 'yes'->to_bool()    // true
echo '0'->to_bool()      // false
Beispiel
<echo>{ 'yes'->to_bool() }</echo>
echo 'yes'->to_bool()

#string->to_date(format?: date_format) → date

Liest den Text als Datum: '2026-11-22'->to_date() oder, mit der Schreibweise, '22-11-2026'->to_date(dd-MM-yyyy).

->to_date(format?) liest Text als Datum. Daten im ISO-Format, etwa '2026-11-22' oder '2026-11-22 14:30', brauchen kein Format; für jede andere Schreibweise geben Sie das Muster an, in dem sie geschrieben ist:

<let name="day" value="'2026-11-22'->to_date()" />
<echo value="$day.year" /> <!-- 2026 -->
<echo value="'22-11-2026'->to_date(dd-MM-yyyy).month" /> <!-- 11 because the pattern says the month is in the middle -->
$day = '2026-11-22'->to_date()
echo $day.year                                // 2026
echo '22-11-2026'->to_date(dd-MM-yyyy).month  // 11 because the pattern says the month is in the middle
ParameterNimmt
format optionaldate_format
Beispiel
<echo>{ '2026-11-19'->to_date().weekday }</echo>
echo '2026-11-19'->to_date().weekday

#string->to_int(default?) → number

Der Text als ganze Zahl, oder default, wenn es keine ist.

->to_int(default?) liest Text als ganze Zahl und verwirft alles nach dem Dezimalpunkt. Ist der Text keine Zahl, erhalten Sie default; ohne Angabe ist das ein Typfehler, daher sollten Sie für unsichere Texte einen Standardwert übergeben:

<echo value="'42'->to_int()" /> <!-- 42 -->
<echo value="'3.7'->to_int()" /> <!-- 3 because the .7 is dropped -->
<echo value="'abc'->to_int(-1)" /> <!-- -1 because abc isn't a number -->
echo '42'->to_int()        // 42
echo '3.7'->to_int()       // 3 because the .7 is dropped
echo 'abc'->to_int(-1)     // -1 because abc isn't a number
Beispiel
<echo>{ '42'->to_int() + 1 }</echo>
echo '42'->to_int() + 1

#string->to_number(default?) → number

Der Text als Zahl, oder default, wenn es keine ist.

->to_number(default?) liest Text als Zahl, mit Nachkommastellen. Ist der Text keine Zahl, erhalten Sie default; ohne Angabe ist das ein Typfehler, der dies anzeigt:

<echo value="'3.7'->to_number()" /> <!-- 3.7 -->
<echo value="'x'->to_number(0)" /> <!-- 0 because x isn't a number, so the default is used -->
<echo value="'x'->to_number(null)" /> <!-- null -->
echo '3.7'->to_number()      // 3.7
echo 'x'->to_number(0)       // 0 because x isn't a number, so the default is used
echo 'x'->to_number(null)    // null
Beispiel
<echo>{ '3.5'->to_number() * 2 }</echo>
echo '3.5'->to_number() * 2

#string->trim(characters?, start?: bool, end?: bool) → string

Der Text ohne Leerzeichen (oder die angegebenen Zeichen) an beiden Enden oder nur am Anfang oder Ende: ->trim(start), ->trim('/', end).

->trim(characters?, start?, end?) entfernt Leerzeichen, Tabulatoren und Zeilenumbrüche an beiden Enden oder die von Ihnen angegebenen Zeichen. Mit start oder end wird nur dieses Ende getrimmt:

<echo value="'  tea  '->trim()" /> <!-- 'tea' -->
<echo value="'//path//'->trim('/')" /> <!-- 'path' -->
<echo value="'  tea  '->trim(start)" /> <!-- 'tea  ' because only the start is trimmed -->
echo '  tea  '->trim()           // 'tea'
echo '//path//'->trim('/')       // 'path'
echo '  tea  '->trim(start)      // 'tea  ' because only the start is trimmed
ParameterNimmt
characters optionalein beliebiger Wert
start optionalein Schalter: start oder not start
end optionalein Flag: end oder not end

Argumente können benannt werden: ->trim(…, start: …, end: …).

Beispiel
<echo>{ '  hello  '->trim() + '!' }</echo>
echo '  hello  '->trim() + '!'

#string->truncate(length: number, suffix?) → string

Kürzt den Text auf length und hängt suffix an (standardmäßig …).

->truncate(length, suffix?) behält die ersten length Zeichen und fügt ein Suffix hinzu, …, sofern Sie kein anderes angeben, wenn es mehr gab. Text, der bereits passt, bleibt unverändert:

<echo value="'Hello there, friend'->truncate(10)" /> <!-- 'Hello ther…' -->
<echo value="'Hello there, friend'->truncate(10, '...')" /> <!-- 'Hello ther...' -->
<echo value="'Hi'->truncate(10)" /> <!-- 'Hi' because it already fits -->
echo 'Hello there, friend'->truncate(10)          // 'Hello ther…'
echo 'Hello there, friend'->truncate(10, '...')   // 'Hello ther...'
echo 'Hi'->truncate(10)                           // 'Hi' because it already fits
ParameterNimmt
length erforderlichnumber
suffix optionalein beliebiger Wert

Argumente können benannt werden: ->truncate(…, suffix: …).

Beispiel
<echo>{ 'A long sentence about nothing'->truncate(12) }</echo>
echo 'A long sentence about nothing'->truncate(12)

#string->upper() → string

Der Text in Großbuchstaben.

->upper() gibt den Text in Großbuchstaben aus:

<echo value="'Green tea'->upper()" /> <!-- 'GREEN TEA' -->
echo 'Green tea'->upper()   // 'GREEN TEA'
Beispiel
<echo>{ 'hello'->upper() }</echo>
echo 'hello'->upper()

#string->url_decode() → string

URL-kodierter Text, dekodiert.

->url_decode() wandelt URL-Escapes wie %20 zurück in die Zeichen, für die sie stehen:

<echo value="'tea%20%26%20milk'->url_decode()" /> <!-- 'tea & milk' -->
echo 'tea%20%26%20milk'->url_decode()   // 'tea & milk'
Beispiel
<echo>{ 'a%20b%2Fc'->url_decode() }</echo>
echo 'a%20b%2Fc'->url_decode()

#string->url_encode() → string

Der Text für eine URL kodiert.

->url_encode() maskiert Text, sodass er sicher in einer URL steht, etwa ein Suchbegriff in einem Query-String:

<echo value="'tea &amp; milk'->url_encode()" /> <!-- 'tea%20%26%20milk' because spaces and & have a meaning in URLs -->
echo 'tea & milk'->url_encode()   // 'tea%20%26%20milk' because spaces and & have a meaning in URLs
Beispiel
<echo>{ 'a b/c?d'->url_encode() }</echo>
echo 'a b/c?d'->url_encode()

#string->words() → string[]

Die Wörter im Text als Liste, ohne Leerzeichen oder Satzzeichen. Buchstaben jeder Schrift zählen, und don't oder well-known gilt als ein Wort.

->words() liefert die Wörter im Text, ohne die Leerzeichen und Satzzeichen dazwischen. Buchstaben jeder Schrift zählen, und don't oder well-known bleiben ein Wort:

<echo value="'Hello, world!'->words()" /> <!-- ['Hello', 'world'] -->
<echo value="&quot;Don't stop&quot;->words()->count()" /> <!-- 2 because don't is one word -->
echo 'Hello, world!'->words()           // ['Hello', 'world']
echo "Don't stop"->words()->count()     // 2 because don't is one word
Beispiel
<echo>{ 'Hello, world! A well-known phrase.'->words() }</echo>
echo 'Hello, world! A well-known phrase.'->words()

#string->wrap(width: number) → string

Der Text mit Zeilenumbrüchen zwischen den Wörtern, sodass keine Zeile länger als width ist (außer ein einzelnes Wort ist länger). Funktioniert gut mit @terminal_width().

->wrap(width) setzt Zeilenumbrüche zwischen Wörter, sodass keine Zeile länger als width ist, sofern nicht ein einzelnes Wort länger ist. Es eignet sich für Text im Terminal, etwa einer Breite von @terminal_width():

<let name="lines" value="'Ann went home early'->wrap(width: 9)->lines()" />
<echo value="$lines" /> <!-- ['Ann went', 'home', 'early'] because home early would be 10 characters -->
$lines = 'Ann went home early'->wrap(width: 9)->lines()
echo $lines   // ['Ann went', 'home', 'early'] because home early would be 10 characters
ParameterNimmt
width erforderlichnumber
Beispiel
<echo>{ 'The quick brown fox jumps over the lazy dog'->wrap(width: 15)->lines() }</echo>
echo 'The quick brown fox jumps over the lazy dog'->wrap(width: 15)->lines()

Aus KScript.Markdown

Dafür benötigen Sie <import package="KScript.Markdown" />import KScript.Markdown. Siehe KScript.Markdown.

#string->highlight(lang?: string) → string KScript.Markdown

Der Code als hervorgehobenes HTML für ein <pre>, eingefärbt mit den Grammatiken der VS-Code-Erweiterung: Jedes Token steht in einem <span class="hl-kw"> (hl-com, hl-str, hl-num, hl-kw, hl-op, hl-fn, hl-type, hl-var, hl-tag, hl-attr, hl-expr, hl-re), damit ein Stylesheet es einfärben kann. lang ist kscript (der Standard), ks.xml, html (eine Seite), ks.css, text oder eine Sprache, die VS Code kennt: css, js, ts, json, sql, sh, yaml, python...
ParameterNimmt
lang optionalstring
Beispiel
<import package="KScript.Markdown" />
<let name="code" value="'$total = $price * 2 // with tax'" />
<echo>{ $code->highlight(lang: 'kscript') }</echo>
import KScript.Markdown
$code = '$total = $price * 2 // with tax'
echo $code->highlight(lang: 'kscript')

#string->markdown_escape() → string KScript.Markdown

Der Text maskiert, damit Markdown ihn so anzeigt, wie er geschrieben ist.
Beispiel
<import package="KScript.Markdown" />
<echo>{ '2 * 3 = 6'->markdown_escape() }</echo>
import KScript.Markdown
echo '2 * 3 = 6'->markdown_escape()

Methoden von any

Methoden von number