Skip to content

trim — Trim ​

17:43

Description ​

$u.trim strips whitespace from the requested positions: both removes both ends (default), left removes the left side, right removes the right side, and all removes every whitespace character.

Examples ​

Basic usage ​

The four pos modes trim the same ' hello ' string at both ends, on the left, on the right or everywhere.

View source
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>

Edge cases & fallback ​

An all-whitespace string and an empty string both return an empty string, while both keeps spaces inside the string.

View source
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 ​

Parameters ​

ParameterDescriptionTypeDefaultVersion
strThe string to processstring--
posTrim position: both for both ends, left, right or all'both' | 'left' | 'right' | 'all''both'-

Returns ​

TypeDescription
stringThe string with whitespace removed

Notes ​

  • both only removes leading and trailing whitespace; spaces inside the string are kept.
  • all removes every whitespace character in the string.
  • An empty string and a whitespace-only string both return an empty string in every mode.