你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

trim()

删除指定正则表达式的所有前导匹配项和尾随匹配项。

语法

trim(regex,source)

详细了解语法约定

参数

名称 类型 必需 说明
regex string ✔️ 要从 source 剪裁的字符串或正则表达式
source string ✔️ 要从中剪裁 regex 的源字符串。

返回

剪裁 source 开头和/或结尾的 regex 匹配项后的 source 。

示例

剪裁特定子字符串

以下语句从 string_to_trim 的开头和结尾剪裁 substring。

let string_to_trim = @"--https://bing.com--";
let substring = "--";
print string_to_trim = string_to_trim, trimmed_string = trim(substring,string_to_trim)

输出

string_to_trim trimmed_string
--<https://bing.com--> <https://bing.com>

剪裁非字母数字字符

下一条语句从字符串的开头和结尾剪裁所有非单词字符。

range x from 1 to 5 step 1
| project str = strcat("-  ","Te st",x,@"// $")
| extend trimmed_str = trim(@"[^\w]+",str)

输出

str trimmed_str
- Te st1// $ Te st1
- Te st2// $ Te st2
- Te st3// $ Te st3
- Te st4// $ Te st4
- Te st5// $ Te st5

剪裁空格

下一个语句剪裁字符串开头和结尾的所有空格。

let string_to_trim = @"    Hello, world!    ";
let substring = @"\s+";
print
    string_to_trim = string_to_trim,
    trimmed_string = trim(substring, string_to_trim)

输出

string_to_trim trimmed_string
Hello, world! Hello, world!