本页内容

字符串函数

用于文本处理、搜索和格式化的函数。

len

(text)

返回字符串的长度,即字符数。

参数
text string 要测量的字符串
返回值 数字
示例
len("Hello") 5
len("") 0

left

(text, count)

返回字符串的前N个字符。

参数
text string 源字符串
count number 要提取的字符数
返回值 字符串
示例
left("ProcessMind", 7) Process
另请参阅
(text, count)

返回字符串的后N个字符。

参数
text string 源字符串
count number 要提取的字符数
返回值 字符串
示例
right("ProcessMind", 4) Mind
另请参阅

mid

(text, start, count)

从字符串中间返回子字符串,从基于1的位置开始,提取指定数量的字符。

参数
text string 源字符串
start number 起始位置(从1开始)
count number 要提取的字符数
返回值 字符串
示例
mid("ProcessMind", 8, 4) Mind
mid("Hello World", 1, 5) Hello
另请参阅

lower

(text)

将字符串转换为小写。

参数
text string 要转换的字符串
返回值 字符串
示例
lower("HELLO") hello
另请参阅

upper

(text)

将字符串转换为大写。

参数
text string 要转换的字符串
返回值 字符串
示例
upper("hello") HELLO
另请参阅

proper

(text)

将字符串转换为首字母大写格式,即每个单词的首字母大写。

参数
text string 要转换的字符串
返回值 字符串
示例
proper("hello world") Hello World
proper("HELLO WORLD") Hello World
另请参阅

trim

(text)

移除字符串两端的空白字符。

参数
text string 要截去空白的字符串
返回值 字符串
示例
trim(" hello ") hello
另请参阅

trimStart

(text)

移除字符串开头的空白字符。

参数
text string 要截去空白的字符串
返回值 字符串
示例
trimStart(" hello") hello
另请参阅

trimEnd

(text)

移除字符串末尾的空白字符。

参数
text string 要截去空白的字符串
返回值 字符串
示例
trimEnd("hello ") hello
另请参阅

replace

(text, search, replacement)

将搜索字符串的每次出现替换为替换字符串。

参数
text string 源字符串
search string 要查找的字符串
replacement string 替换字符串
返回值 字符串
示例
replace("Hello World", "World", "ProcessMind") Hello ProcessMind
replace("a-b-c", "-", "_") a_b_c

concat

(text1, text2, ...?)

连接两个或多个字符串。

参数
text1 string 第一个字符串
text2 string 第二个字符串
... (可选) string 其他字符串,最多共6个
返回值 字符串
示例
concat("Hello", " ", "World") Hello World
备注

也可以使用+运算符连接字符串。

contains

(text, search)

检查字符串是否包含另一个字符串。

参数
text string 要搜索的字符串
search string 要查找的字符串
返回值 boolean
示例
contains("ProcessMind", "Mind") true
contains("Hello", "xyz") false
另请参阅

startsWith

(text, prefix)

检查字符串是否以指定前缀开头。

参数
text string 要检查的字符串
prefix string 要查找的前缀
返回值 boolean
示例
startsWith("ProcessMind", "Process") true
另请参阅

endsWith

(text, suffix)

检查字符串是否以指定后缀结尾。

参数
text string 要检查的字符串
suffix string 要查找的后缀
返回值 boolean
示例
endsWith("ProcessMind", "Mind") true
另请参阅

indexOf

(text, search)

返回第一个子字符串出现位置的1-based索引;如果未找到,则返回0。

参数
text string 要搜索的字符串
search string 要查找的字符串
返回值 数字
示例
indexOf("ProcessMind", "Mind") 8
indexOf("Hello", "xyz") 0
另请参阅

find

(search, text)

indexOf的别名。返回第一个子字符串出现位置的1-based索引。

参数
search string 要查找的字符串
text string 要搜索的字符串
返回值 数字
示例
find("Mind", "ProcessMind") 8
备注

请注意,与indexOf相比,参数顺序相反。

另请参阅

padLeft

(text, length, char?)

在字符串左侧填充字符,直到达到指定长度。

参数
text string 要填充的字符串
length number 目标长度
char (可选) string 用于填充的字符 (默认值: " "(空格))
返回值 字符串
示例
padLeft("42", 5, "0") 00042
padLeft("Hi", 5) Hi
另请参阅

padRight

(text, length, char?)

在字符串右侧填充字符,直到达到指定长度。

参数
text string 要填充的字符串
length number 目标长度
char (可选) string 用于填充的字符 (默认值: " "(空格))
返回值 字符串
示例
padRight("Hi", 5, ".") Hi...
另请参阅

相关主题