KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

stringのフィールドとメソッド

テキスト。数値を表す文字列では、numberのメソッドも使えます。

テキストはstringです:引用符で囲まれたものすべて。'Single quotes'入力した内容はそのまま保持され、"double quotes"は{ }内の値で埋められるため、"Hello { $name }"で変数を読み取れます。複数行のテキストには、```の間にテキストブロックを書きます。これにより、すべての改行がそのまま保持されます。

テキストのメソッドは、呼び出し元のテキストを変更しません。新しいテキスト(または数値、リスト、bool)を返すため、連結して使うか、結果を変数に入れて使ってください:

<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

位置は0から数えます。負の位置は末尾から数えます。絵文字などの文字は2つの位置として数えられ、どのメソッドも途中で切ることはありません。比較では、ignore_caseフラグを指定しない限り大文字と小文字が厳密に区別されます。

テキストについて調べる: count · count_of · is_empty · is_blank · is_numeric · contains · starts_with · ends_with · index_of · matches

一部を取り出す: first · last · slice · before · after · truncate · find_all

整形・変換する: trim · squish · strip · replace · upper · lower · capitalize · title · case · pad · repeat · reverse · wrap · mask · normalize_unicode

リストに分割する: split · lines · blocks · words · chars · numbers · grid · shapes

別の値として読み取る: to_number · to_int · to_bool · to_date、または->convert()で別の形式に変換

送信に安全な形にする: escape · url_encode · url_decode · to_base64 · from_base64 · md5 · sha1 · sha256 · sha512 · hmac

フィールドは、値についての単純な事実で、ドットを使い角括弧なしで読み取ります: $value.name。メソッドは引数を取るか処理を行い、-> で呼び出します: $value->name(arguments)。または、値をパイプで渡して呼び出します: $value |> name(arguments)。

KScript.Core から

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

文字列の最初の出現(またはlastを指定した場合は最後の出現)より後ろのテキスト。見つからない場合は全体を返します: 'a.tar.gz'->after('.', last) は gz です。

->after(text, last?) は、text が最初に現れる位置より後ろのテキストを返します。last を指定すると、最後に現れる位置より後ろのテキストを返します。text がない場合は、テキスト全体が返されます:

<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
パラメーター受け取るもの
text 必須string
last optionalフラグ: last または not last

引数には名前を付けることができます: ->after(…, last: …)。

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

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

文字列の最初の出現(またはlastを指定した場合は最後の出現)より前のテキスト。見つからない場合は全体を返します: 'a.tar.gz'->before('.', last) は a.tar です。

->before(text, last?) は、text が最初に現れる位置より前のテキストを返します。last を指定すると、最後に現れる位置より前のテキストを返します。text がない場合は、テキスト全体が返されます:

<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
パラメーター受け取るもの
text 必須string
last optionalフラグ: last または not last

引数には名前を付けることができます: ->before(…, last: …)。

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

#string->blocks() → string[]

空行で区切られたテキストの各部分。各部分の前後の改行は取り除かれます: 'a\nb\n\nc'->blocks() は ['a\nb', 'c'] です。

->blocks() はテキストを空行で分割するので、各ブロックは複数の行のまとまりになります。改行1つではブロックは分かれず、空行のときだけ新しいブロックが始まります。各ブロックの前後の改行は取り除かれます:

<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
例
<echo>{ ('a' + @char_of(10) + @char_of(10) + 'b')->blocks() }</echo>
echo ('a' + @char_of(10) + @char_of(10) + 'b')->blocks()

#string->capitalize() → string

先頭の文字を大文字にしたテキストです。

->capitalize() は先頭の文字を大文字にし、残りはそのままにします:

<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'
例
<echo>{ 'hello world'->capitalize() }</echo>
echo 'hello world'->capitalize()

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

テキストを、指定したスタイルの識別子として変換します: 'User ID'->case(snake) は user_id、kebab は user-id、camel は userId、pascal は UserId になります。

->case(style) はテキストを指定したスタイルの名前に書き換えます: snake は user_id、kebab は user-id、camel は userId、pascal は UserId になります。スペース、区切り文字、大文字と小文字の切り替わりのいずれでも単語を見つけるので、どのスタイルからどのスタイルにも変換できます:

<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'
パラメーター受け取るもの
style 必須次のいずれかの語: snake|kebab|camel|pascal
例
<echo>{ 'Order Total'->case(snake) }</echo>
echo 'Order Total'->case(snake)

#string->chars() → string[]

文字を、リストとして返します。

->chars() はテキストの文字を、それぞれ1つのテキストとしてリストで返します。絵文字などの文字は、どのように格納されていても1つの要素になります:

<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
例
<echo>{ 'abc'->chars() }</echo>
echo 'abc'->chars()

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

テキストにこのテキストが含まれる場合にtrueを返します。ignore_case: trueの場合、大文字と小文字を区別しません。

->contains(text, ignore_case?) は、テキストのどこかに text が含まれていれば true になります。ignore_case を指定すると、大文字と小文字を区別せずに比較します:

<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
パラメーター受け取るもの
text 必須任意の値
ignore_case 省略可能フラグ: ignore_case または not ignore_case

引数には名前を付けることができます: ->contains(…, ignore_case: …)。

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

#string->count() → number

文字数です。

->count() はテキストの文字数を返します:

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

#string->count_of(match) → number

文字列内の各テキストが出現する回数です。

->count_of(match) は、テキストの一部が出現する回数を数えます。出現箇所は重なりません:

<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
例
<echo>{ 'banana'->count_of('a') }</echo>
echo 'banana'->count_of('a')

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

テキストが指定したテキストで終わる場合は true を返します。ignore_case: true を指定すると、大文字と小文字を区別しません。

->ends_with(text, ignore_case?) は、テキストが text で終わっていれば true になります:

<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
パラメーター受け取るもの
text 必須任意の値
ignore_case 省略可能フラグ: ignore_case または not ignore_case

引数には名前を付けることができます: ->ends_with(…, ignore_case: …)。

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

#string->escape() → string

HTML エスケープしたテキストです。

->escape() は、テキストをHTMLに安全に埋め込めるようにします。<、>、& と引用符はエンティティに変換されるため、ユーザーが入力した内容はマークアップとしてではなくテキストとして表示されます:

<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;'

ページは出力する内容を既にエスケープしています。これは自分で組み立てるHTML向けの機能です。

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

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

正規表現に一致するすべての結果をリストで返します。

->find_all(pattern) は正規表現に一致するものをすべて、出現順に返します:

<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() は、数値そのものを数値として返します。

パラメーター受け取るもの
pattern 必須正規表現
例
<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

最初の文字。

->first() は先頭の文字を返します。空のテキストの場合は '' を返します:

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

#string->from_base64() → string

デコードされた Base64 テキストです。

->from_base64() は base64 のテキストを、元のテキストに戻します。base64 ではないテキストはエラーになります:

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

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

テキストを文字の行の配列として返します(1行ごとのリスト)。マップや迷路に使えます: '#.\n.#'->grid() は [['#', '.'], ['.', '#']] になります。末尾の改行では空の行は追加されません。セルは $grid[row][column] で読み取り、行や列を ->cells(rows) または ->cells(columns) で順にたどれます。

->grid() は文字の行を、1文字ずつの行の配列に変換します。したがって $grid[row][column] は1文字です。行0は一番上の行、列0は先頭の文字です。末尾の改行では空の行は追加されません:

<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

行や列を順にたどるには、->cells(rows) または ->cells(columns) でグリッドを読み取ります。

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

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

秘密鍵を使ったテキストのHMACを16進数で返します(既定はSHA-256)。Webhookの署名を作るときに使います。署名の比較には @secure_equals() を使ってください。

->hmac(key, algorithm?) は秘密鍵でテキストに署名し、16進数の署名を返します。既定は SHA-256 ですが、sha1 または sha512 を渡して変更できます。Webhook が送信元を確認するために使われます。署名の比較には @secure_equals() を使ってください。一致する部分がどれだけあっても、同じ時間で比較されます:

<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
パラメーター受け取るもの
key 必須任意の値
algorithm 省略可次のいずれかの語: sha256|sha1|sha512

引数には名前を付けることができます: ->hmac(…, algorithm: …)。

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

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

テキストが最初に現れる位置を返します。lastを指定すると最後に現れる位置を返します。見つからない場合は -1 を返します。ignore_case を指定すると、大文字と小文字を区別せずに比較します。

->index_of(text, ignore_case?, last?) は text が最初に現れる位置を返します。last を指定すると、最後に現れる位置を返します。text が見つからない場合は -1 を返します:

<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
パラメーター受け取るもの
text 必須任意の値
ignore_case 省略可能フラグ: ignore_case または not ignore_case
last optionalフラグ: last または not last

引数には名前を付けることができます: ->index_of(…, ignore_case: …, last: …)。

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

#string->is_blank() → bool

テキストが空、または空白文字のみの場合に true を返します。

->is_blank() は、テキストが空であるか、スペース、タブ、改行だけで構成されている場合に true になります。フォームに入力された内容を確認するときに使います:

<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
例
<echo>{ '   '->is_blank() }</echo>
echo '   '->is_blank()

#string->is_empty() → bool

テキストの文字数がゼロの場合に true を返します。

->is_empty() は、テキストに文字が1つもない場合に true になります。スペースも文字として数えるため、' ' は空ではありません。空かどうかを判定するには ->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
例
<echo>{ ''->is_empty() }</echo>
echo ''->is_empty()

#string->is_numeric() → bool

数値だけで構成されたテキストの場合は true になります。例えば '42'、'-3.5'、'1e3' などです。前後の空白は許容されます。

->is_numeric() は、テキスト全体が '42'、'-3.5'、'1e3' などの数値だけの場合に true になります。前後の空白は許容されます:

<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
例
<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

最後の文字です。

->last() は最後の文字を返します。空のテキストの場合は '' を返します:

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

#string->lines() → string[]

テキストの行を、リストとして返します。

->lines() は行ごとのテキストを返します。空行は空のテキストになるため、グループ間の区切りを見つけられます:

<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() は、代わりに空行で分割します。

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

#string->lower() → string

小文字にした文字列です。

->lower() は小文字にしたテキストを返します:

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

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

テキストの各文字を char(既定では *)に置き換えたものを返します。ただし末尾の keep_end 文字は残します。例えば ->mask('*', keep_end: 4) は末尾の 4 文字だけを表示します。

->mask(char?, keep_end?) は、テキストを文字で隠します。別の文字を指定しない限り * を使い、末尾の keep_end 文字は残すので、読み手はそれが何か見分けられます:

<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()                             // '******'
パラメーター受け取るもの
char optionalstring
keep_end optionalnumber

引数には名前を付けられます: ->mask(…, keep_end: …)。

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

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

テキストが正規表現に一致する場合に true になります。例:->matches(/^[a-z]+$/i)。

->matches(pattern) は、正規表現がテキスト内で一致する場合に true になります。テキスト全体を検査するには ^ と $ で囲みます:

<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
パラメーター受け取るもの
pattern 必須正規表現
例
<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

テキストの MD5 ハッシュを 16 進数で表したものです。

->md5() はテキストの MD5 ハッシュを 16 進数で返します。キャッシュキーやチェックサムには手軽で十分ですが、パスワードや署名には向きません:

<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
例
<echo>{ 'hello'->md5() }</echo>
echo 'hello'->md5()

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

指定した形式(既定では NFC)で Unicode を正規化します。

->normalize_unicode(form?) は見た目が同じテキストを同じものとして比較できるようにします。é は 1 文字としても、e とアクセント記号の組み合わせとしても保存できます。NFC(既定)はそれらを結合し、NFD は分解します:

<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
パラメーター受け取るもの
form optional次のいずれかの語: NFC|NFD|NFKC|NFKD
例
<echo>{ ('e' + @char_of(769))->normalize_unicode() }</echo>
echo ('e' + @char_of(769))->normalize_unicode()

#string->numbers() → number[]

テキスト中のすべての数値を、出現順に数値として返します。'x=-3, y=12'->numbers() は [-3, 12] になります。文字や数字の直前にマイナスがない場合のみ負の数になるので、'1-3'->numbers() は [1, 3] になります。

->numbers() は、テキスト中に書かれたすべての数値を出現順に数値として返します。マイナス記号は、左側に何も連結されていない場合に符号として扱われます(x=-3 の場合など)。2 つの数値の間にある場合(1-3 の場合など)は、ダッシュとして扱われます:

<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]
例
<echo>{ 'x=-3, y=12'->numbers() }</echo>
echo 'x=-3, y=12'->numbers()

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

テキストを width になるまで末尾に詰めたものを返します。start を指定した場合は先頭に詰めます。例えば '7'->pad(3, '0', start) は 007 になります。

->pad(width, char?, start?) は、テキストが width 文字の長さになるまで文字を追加します。別の文字を指定しない限り空白を使います。末尾に詰めるか、start を指定すると先頭に詰めます。すでにその長さ以上のテキストはそのままにします:

<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
パラメーター受け取るもの
width requirednumber
char optional任意の値
start optionalフラグ: start または not start

引数には名前を付けられます: ->pad(…, char: …, start: …)。

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

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

テキストを count 回繰り返したものです。

->repeat(count) はテキストを count 回繰り返したものを返します:

<echo value="'ha'->repeat(3)" /> <!-- 'hahaha' -->
<echo value="'-'->repeat(10)" /> <!-- '- -- -- -- -- -' -->
echo 'ha'->repeat(3)    // 'hahaha'
echo '-'->repeat(10)    // '----------'
パラメーター受け取るもの
count 必須number
例
<echo>{ 'ab'->repeat(3) }</echo>
echo 'ab'->repeat(3)

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

テキスト中のすべての old を new に置き換えたものを返します。old は正規表現にできます: ->replace(/\s+/, ' ')。new ではグループを $1 または ${name} として使えます。ignore_case を指定すると、通常のテキストは大文字・小文字を区別せずに一致します。

->replace(old, new, ignore_case?) は、すべての old を new に置き換えます。old は正規表現にでき、その場合 new では一致したグループを $1 または ${name} として使えます:

<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
パラメーター受け取るもの
old required任意の値
new required任意の値
ignore_case 省略可能フラグ: ignore_case または not ignore_case

引数には名前を付けられます: ->replace(…, new: …, ignore_case: …)。

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

#string->reverse() → string

逆順にしたテキストです。

->reverse() は文字の順序を逆にしたものを返します。各文字はそのまま保持します:

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

#string->sha1() → string

テキストの SHA-1 ハッシュを 16 進数で返します。SHA-1 を要求する他のシステムでのみ使用してください。

->sha1() はテキストの SHA-1 ハッシュを 16 進数(40 文字)で返します。新しいコードでは ->sha256() を優先してください:

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

#string->sha256() → string

テキストの SHA-256 ハッシュを 16 進数で返します。

->sha256() はテキストの SHA-256 ハッシュを 16 進数(64 文字)で返します。同じテキストからは常に同じハッシュが得られ、変更があれば別のハッシュになります:

<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
例
<echo>{ 'hello'->sha256() }</echo>
echo 'hello'->sha256()

#string->sha512() → string

テキストの SHA-512 ハッシュを 16 進数で返します。

->sha512() はテキストの SHA-512 ハッシュを 16 進数(128 文字)で返します:

<echo value="'tea'->sha512()->count()" /> <!-- 128 -->
echo 'tea'->sha512()->count()   // 128
例
<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
パラメーター受け取るもの
mask 任意any
square 任意フラグ: square または not square
rectangle 任意フラグ: rectangle または not rectangle
line 任意フラグ: line または not line
circle 任意フラグ: circle または not circle
of 任意any
rotated 任意フラグ: rotated または not rotated
size optionalnumber
radius 任意number
distance 任意次のいずれかの単語: straight|steps|moves

引数には名前を付けることができます: ->shapes(…, square: …, rectangle: …)。

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

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

テキストの一部です。負の位置は末尾からの位置を表します。

->slice(start, end?) は位置 start から、end を含まない位置までの文字を返します。あるいは末尾までを返します。負の位置は末尾からの数え方になります:

<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
パラメーター受け取るもの
start 必須number
end 任意number

引数には名前を付けることができます: ->slice(…, end: …)。

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

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

各区切り文字(または空白)の間の部分をリストとして返します。区切り文字は正規表現にできます: ->split(/\s*,\s*/)。

->split(separator?) は、各区切り文字でテキストを切り、その部分をリストとして返します。区切り文字を指定しない場合は空白で切ります。区切り文字は正規表現にできます:

<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
例
<echo>{ 'a,b,c'->split(',') }</echo>
echo 'a,b,c'->split(',')

#string->squish() → string

文字列の前後の空白を取り除き、内部の連続する空白、タブ、改行を 1 つの空白にまとめます。

->squish() はテキストの前後の空白を取り除き、内部の空白・タブ・改行の連続を 1 つの空白にします:

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

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

文字列がこの文字列で始まるかどうかを返します。ignore_case: true を指定すると、大文字と小文字を区別せずに比較します。

->starts_with(text, ignore_case?) は、テキストが text で始まる場合に true になります:

<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
パラメーター受け取るもの
text 必須任意の値
ignore_case 省略可能フラグ: ignore_case または not ignore_case

引数には名前を付けられます: ->starts_with(…, ignore_case: …)。

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

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

完全に一致する接頭辞、接尾辞、またはその両方を、存在する場合に取り除いたテキスト: 'v1.2'->strip(prefix: 'v')、'a.txt'->strip(suffix: '.txt')。

->strip(prefix?, suffix?) は、完全に一致する接頭辞、接尾辞、またはその両方を、存在する場合に 1 回だけ取り除きます。->trim() と違い、文字ごとではなく、まとまり全体を取り除きます:

<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
パラメーター受け取るもの
prefix 省略可能string
suffix 省略可能string

引数には名前を付けられます: ->strip(…, suffix: …)。

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

#string->title() → string

テキストを Title Case(単語の先頭を大文字)にします。

->title() は、テキストを Title Case にします。各単語の先頭の文字を大文字にします:

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

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

テキストを base64 でエンコードしたもの。url: true を指定すると、パディングなしの URL セーフな base64 になります。

->to_base64(url?) はテキストを base64 でエンコードします。base64 は文字、数字、+、/、= だけを使うため、HTTP ヘッダーのように他の文字を使えない場所でも安全に扱えます。url を指定すると、代わりに - と _ を使い、= のパディングを付けないため、URL に入れられます。結果は base64 型の値で、その ->decode() でテキストを元に戻せます:

<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
パラメーター受け取るもの
url 省略可能フラグ: url または not url
例
<echo>{ 'hello'->to_base64() }</echo>
echo 'hello'->to_base64()

#string->to_bool() → bool

'true'、'false'、'yes'、'no'、'1'、'0' などのテキストを bool として読み取ります。

->to_bool() は、'true'、'false'、'yes'、'no'、'1'、'0' などのテキストを bool として読み取ります。それ以外のテキストは型エラーになります:

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

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

テキストを日付として読み取ります: '2026-11-22'->to_date()、または書式に合わせて '22-11-2026'->to_date(dd-MM-yyyy)。

->to_date(format?) はテキストを日付として読み取ります。'2026-11-22' や '2026-11-22 14:30' のような ISO 形式の日付には書式の指定は要りません。それ以外の書き方の場合は、書かれている形式のパターンを指定してください:

<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
パラメーター受け取るもの
format 任意date_format
例
<echo>{ '2026-11-19'->to_date().weekday }</echo>
echo '2026-11-19'->to_date().weekday

#string->to_int(default?) → number

テキストを整数として読み取った値。整数でない場合は default の値。

->to_int(default?) はテキストを整数として読み取り、小数点以下は切り捨てます。テキストが数値でない場合は default になります。default がないと型エラーになるため、確信が持てないテキストには既定値を渡してください:

<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
例
<echo>{ '42'->to_int() + 1 }</echo>
echo '42'->to_int() + 1

#string->to_number(default?) → number

テキストを数値として読み取った値。小数も含みます。数値でない場合は default の値。

->to_number(default?) はテキストを数値として読み取り、小数も扱います。テキストが数値でない場合は default になります。default がないと、そのことを示す型エラーになります:

<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
例
<echo>{ '3.5'->to_number() * 2 }</echo>
echo '3.5'->to_number() * 2

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

テキストの両端から空白(または指定した文字)を取り除いた値。先頭または末尾だけ取り除くこともできます: ->trim(start)、->trim('/', end)。

->trim(characters?, start?, end?) は、テキストの両端から空白、タブ、改行を取り除きます。指定した文字を取り除くこともできます。start または end を指定すると、その端だけを取り除きます:

<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
パラメーター受け取るもの
characters 省略可能任意の値
start optionalフラグ: start または not start
end 任意フラグ: end または not end

引数には名前を付けられます: ->trim(…, start: …, end: …)。

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

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

テキストを length に収まるよう短くし、suffix(既定では …)を付けます。

->truncate(length, suffix?) は先頭の length 文字を残し、それより長い場合は接尾辞を付けます。接尾辞は … ですが、別の値を指定することもできます。収まるテキストはそのままにします:

<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
パラメーター受け取るもの
length 必須number
suffix 省略可能任意の値

引数には名前を付けられます: ->truncate(…, suffix: …)。

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

#string->upper() → string

大文字にしたテキストです。

->upper() はテキストを大文字にします:

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

#string->url_decode() → string

URL エンコードされたテキストをデコードしたものです。

->url_decode() は、%20 のような URL のエスケープを、表している文字に戻します:

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

#string->url_encode() → string

URL 用にエスケープされたテキストです。

->url_encode() はテキストを URL 内で安全に使えるようにエスケープします。たとえばクエリ文字列の検索語などに使います:

<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
例
<echo>{ 'a b/c?d'->url_encode() }</echo>
echo 'a b/c?d'->url_encode()

#string->words() → string[]

テキスト内の単語を、空白や句読点を除いてリストで返します。どの文字体系の文字も数え、don't や well-known は 1 語として扱います。

->words() は、テキスト内の単語を、間の空白や句読点を除いて返します。どの文字体系の文字も数えられ、don't や well-known は 1 語のままです:

<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
例
<echo>{ 'Hello, world! A well-known phrase.'->words() }</echo>
echo 'Hello, world! A well-known phrase.'->words()

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

単語の間に改行を入れて、どの行も width を超えないようにしたテキストを返します(ただし 1 語だけで超える場合を除きます)。@terminal_width() と組み合わせると便利です。

->wrap(width) は単語の間に改行を入れ、どの行も width を超えないようにします。ただし 1 語だけで超える場合は除きます。端末向けのテキスト、たとえば @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
パラメーター受け取るもの
width requirednumber
例
<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()

KScript.Markdown より

these には <import package="KScript.Markdown" />import KScript.Markdown が必要です。KScript.Markdown を参照してください。

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

<pre> 用にハイライトした HTML としてコードを返します。色付けには VS Code 拡張機能が使う文法を使います。各トークンは <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)。スタイルシートで色を付けられます。lang には kscript(既定)、ks.xml、html(ページ全体)、ks.css、text、または VS Code が認識する言語(css、js、ts、json、sql、sh、yaml、python など)を指定します...
パラメーター受け取るもの
lang 省略可能string
例
<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

Markdown でそのまま表示されるように、エスケープしたテキストです。
例
<import package="KScript.Markdown" />
<echo>{ '2 * 3 = 6'->markdown_escape() }</echo>
import KScript.Markdown
echo '2 * 3 = 6'->markdown_escape()

any のメソッド

number のメソッド