KScript 文档
ZH

string 字段和方法

文本。保存数字的字符串也可以使用数字方法。

文本是一个 string:任何放在引号中的内容。'Single quotes' 会保持你所写的内容不变,"double quotes" 会填入 { } 中的内容,因此 "Hello { $name }" 会读取该变量。对于多行文本,请在 ``` 行之间编写文本块,这会保留你所写的每一个换行。

文本方法从不改变调用它们的文本;每个方法都会返回新的文本(或数字、列表、布尔值),因此可以链式调用,或将结果保存在变量中:

<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 开始计数,负数位置从末尾开始计数。像表情符号这样的字符占两个位置,且没有任何方法会将其切成两半。除非传入 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 可选标志参数: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 可选标志参数: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() 按空行把文本拆分成段,每一段就是一组行。单个换行符保留在段落内部,只有空行才会开始新的段落。每段都会去掉首尾的换行:

<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() 将文本拆分为字符列表,每个元素是一个字符的文本。像表情符号这样的字符即使以多个编码单元存储,也只算一项:

<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 时忽略大小写。

只要文本中任意位置包含 text,->contains(text, ignore_case?) 就返回 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,则大小写不敏感匹配。

当文本以 text 结尾时,->ends_with(text, ignore_case?) 返回 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[][]

将文本转换为字符行的二维列表,每行一个列表,用于地图和迷宫:'#.\n.#'->grid() 的结果是 [['#', '.'], ['.', '#']]。末尾的换行不会增加空行。使用 $grid[row][column] 读取某个单元格,使用 ->cells(rows) 或 ->cells(columns) 遍历其行或列。

->grid() 将多行字符转换为由单个字符组成的行,因此 $grid[row][column] 是一个字符。第 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,以十六进制返回(默认 SHA-256),与 webhook 签名的生成方式相同。请使用 @secure_equals() 比较签名。

->hmac(key, algorithm?) 使用密钥对文本签名,并以十六进制返回签名,除非传入 sha1 或 sha512,否则默认使用 SHA-256。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 可选标志参数: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() 返回 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

当整个文本是一个数字时为真,例如 '42'、'-3.5' 或 '1e3'。允许数字前后有空格。

当整个文本是一个数字时,->is_numeric() 为真,例如 '42'、'-3.5' 或 '1e3'。数字前后允许有空格:

<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) 只显示最后四个字符。

->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 可选string
keep_end 可选number

参数可以命名:->mask(…, keep_end: …)。

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

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

当文本匹配正则表达式时为真:->matches(/^[a-z]+$/i)。

当正则表达式在文本中找到匹配时,->matches(pattern) 为真。用 ^ 和 $ 锚定它,以测试整个文本:

<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 哈希值,以十六进制表示。

->md5() 以十六进制返回文本的 MD5 哈希值。它速度快,适合用作缓存键或校验和,但不适用于密码或签名:

<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

使用指定形式规范化 Unicode(默认为 NFC)。

->normalize_unicode(form?) 使外观相同的文本比较结果相同。é 可以存储为一个字符,也可以存储为 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 可选以下单词之一: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;在两个数字之间时,如 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

在末尾填充到指定宽度的文本;若使用 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 必填number
char 可选任意值
start 可选一个标志: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 必填任意值
new 必填任意值
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 哈希值,以十六进制表示。仅在其他系统要求 SHA-1 时使用。

->sha1() 以十六进制返回文本的 SHA-1 哈希值,共 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 哈希值,以十六进制表示。

->sha256() 以十六进制返回文本的 SHA-256 哈希值,共 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 哈希值,以十六进制表示。

->sha512() 以十六进制返回文本的 SHA-512 哈希值,共 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 可选number
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

去掉文本首尾的空白,并将其中连续的空格、制表符和换行合并为一个空格。

->squish() 去除文本首尾空白,并将其中每一连串的空格、制表符和换行符替换为一个空格:

<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

当文本以此文本开头时为 true。若设置 ignore_case: true,则大小写不敏感。

->starts_with(text, ignore_case?) 在文本以 text 开头时为真:

<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?) 会去掉一个完整的前缀、后缀或两者(如果存在)。与 ->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() 将文本转为标题格式:每个单词的首字母大写:

<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,它只使用字母、数字、+、/ 和 =,因此可以在其他字符不适用的地方传输,例如 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' 这样的文本转为布尔值。

->to_bool() 会将 'true'、'false'、'yes'、'no'、'1' 或 '0' 这样的文本读取为布尔值。其他文本会产生类型错误:

<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?) 将文本读取为日期。ISO 格式的日期(如 '2026-11-22' 或 '2026-11-22 14:30')无需指定格式;其他格式则需给出其书写模式:

<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;若未提供默认值,则会产生类型错误,因此对于不确定的文本应传入默认值:

<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;若未提供默认值,则会产生类型错误并说明原因:

<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 可选一个标志: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() 将 URL 转义(如 %20)还原为其所代表的字符:

<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 算作一个单词。

->words() 返回文本中的单词,不含其间的空格和标点。任何文字的字母都计入,don't 或 well-known 保持为一个单词:

<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(除非单个单词本身更长)。适用于 @terminal_width() 的结果。

->wrap(width) 在单词之间插入换行,使每行长度不超过 width(除非单个单词本身更长)。适用于终端显示的文本,例如宽度为 @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 必填number
示例
<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

这些需要 <import package="KScript.Markdown" />import KScript.Markdown。参见 KScript.Markdown。

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

将代码转为带语法高亮的 HTML,用于 <pre>,使用 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 的方法