外观
trim 去除空格
约 219 字小于 1 分钟
2024-01-01
介绍
按指定方向去除字符串中的空白字符,支持去首尾、仅去左侧、仅去右侧或去除全部空白。
基本用法
import { trim } from '@/uni_modules/uview-unix'
trim(' hello ') // 'hello'
trim(' hello ', 'left') // 'hello '
trim(' hello ', 'right') // ' hello'
trim('a b c', 'all') // 'abc'
trim(' hello ', 'other') // ' hello ',未知位置原样返回trim(str: string, pos?: string): string
参数
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| str | 待处理的字符串 | string | - |
| pos | 去除位置:both 首尾、left 左侧、right 右侧、all 全部 | string | 'both' |
返回值
string:处理后的字符串。pos 不在上述四个取值内时原样返回。
注意事项
pos为'both'时使用正则/^\s+|\s+$/g,会连续去除首尾的所有空白字符。all会删除字符串内部的所有空白,包括单词之间的空格。
