Skip to content

trim 去除空格 ​

17:43

说明 ​

$u.trim 按指定位置去除字符串空白:both 去两端(默认)、left 去左侧、right 去右侧、all 去掉全部空白。

示例 ​

基础用法 ​

四种 pos 模式对同一字符串 ' hello ' 分别去两端、左侧、右侧与全部空白。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item title="$u.trim(' hello ')" :value="$u.trim(' hello ')" :arrow="false" />
        <ux-cell-item title="$u.trim(' hello ', 'left')" :value="$u.trim(' hello ', 'left')" :arrow="false" />
        <ux-cell-item title="$u.trim(' hello ', 'right')" :value="$u.trim(' hello ', 'right')" :arrow="false" />
        <ux-cell-item title="$u.trim(' h e l l o ', 'all')" :value="$u.trim(' h e l l o ', 'all')" :arrow="false" :border-bottom="false" />
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
</script>

边界与回退 ​

全空白串与空串都返回空串,both 模式保留字符串中间的空格。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item title="$u.trim('   ')" :value="$u.trim('   ')" :arrow="false" />
        <ux-cell-item title="$u.trim('')" :value="$u.trim('')" :arrow="false" />
        <ux-cell-item title="$u.trim('a b c')" :value="$u.trim('a b c')" :arrow="false" :border-bottom="false" />
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
</script>

API ​

参数 ​

参数说明类型默认值版本
str待处理的字符串string--
pos去除位置:both 两端、left 左侧、right 右侧、all 全部'both' | 'left' | 'right' | 'all''both'-

返回值 ​

类型说明
string去除空白后的字符串

注意事项 ​

  • both 只去掉首尾空白,字符串中间的空格会保留。
  • all 会去掉字符串内部的所有空白字符。
  • 空串与全空白字符串在各模式下都返回空串。