trim — Trim
17:43Description
$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
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| str | The string to process | string | - | - |
| pos | Trim position: both for both ends, left, right or all | 'both' | 'left' | 'right' | 'all' | 'both' | - |
Returns
| Type | Description |
|---|---|
string | The string with whitespace removed |
Notes
bothonly removes leading and trailing whitespace; spaces inside the string are kept.allremoves every whitespace character in the string.- An empty string and a whitespace-only string both return an empty string in every mode.