string — String Utilities
17:43Description
$u.strOr returns the first non-empty string by priority for multi-level fallbacks, while $u.guid generates a random identifier with a given length and character set, defaulting to 32 characters starting with u.
Examples
Multi-level fallback
strOr returns the first non-empty string in argument order, skipping empty strings and null, and returns an empty string when everything is empty.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in strOrList"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type Row = {
desc: string;
result: string;
last: boolean;
};
// 按顺序返回第一个非空字符串,空串视为未传
const strOrList: Row[] = [
{ desc: "$u.strOr(null, 'default')", result: $u.strOr(null, 'default'), last: false },
{ desc: "$u.strOr('', $u.color.primary)", result: $u.strOr('', $u.color.primary), last: false },
// 支持三级及更多级兜底
{ desc: "$u.strOr(null, null, 'fallback')", result: $u.strOr(null, null, 'fallback'), last: false },
{ desc: '$u.strOr(null, $u.color.tipsColor)', result: $u.strOr(null, $u.color.tipsColor), last: false },
// 全部为空时返回空串
{ desc: '$u.strOr(null, null)', result: '-', last: true }
];
</script>Unique identifier
Compares the default 32-character $u.guid, a fixed length, firstU disabled, and radix set to 16 for a hexadecimal character set; each click regenerates the values.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in guidRows"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
<ux-gap :height="12" />
<ux-button size="small" type="primary" block text="重新生成" @click="refreshGuid()" />
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type Row = {
desc: string;
result: string;
last: boolean;
};
const guidRows = ref<Row[]>([]);
function buildGuidRows(): Row[] {
return [
// 默认 32 位,首字符强制为 u
{ desc: '$u.guid()', result: $u.guid(), last: false },
{ desc: '$u.guid(8)', result: $u.guid(8), last: false },
// 第二个参数传 false 则不强制 u 开头,可能以数字开头
{ desc: '$u.guid(8, false)', result: $u.guid(8, false), last: false },
// 第三个参数为进制,16 表示十六进制字符集
{ desc: '$u.guid(16, true, 16)', result: $u.guid(16, true, 16), last: true }
];
}
// 每次点击重算,数据放 ref 才能驱动视图更新
function refreshGuid(): void {
guidRows.value = buildGuidRows();
}
refreshGuid();
</script>API
strOr
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| ...values | Values to check, in priority order from first to last | any | string | null | - | - |
Returns:
| Type | Description |
|---|---|
string | The first non-empty string; returns '' when all values are empty |
guid
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| len | Length of the generated string | number | 32 | - |
| firstU | Whether to force the first character to u | boolean | true | - |
| radix | Character-set radix, which limits the characters used | number | - | - |
Returns:
| Type | Description |
|---|---|
string | The generated random identifier |
Notes
strOronly accepts non-empty strings; empty strings,null,undefinedand non-string values such as numbers are skipped.guidgenerates 32 characters by default and forces the first character tou, preventing an invalid id / class selector that starts with a digit.- When
firstUisfalse, the first character may be a digit, so the result is unsuitable as an id or selector. radixsupports 2-62 (the character set is0-9A-Za-z), and an omitted or0value is treated as 62; results come fromMath.randomand are not guaranteed to be absolutely unique.