Skip to content

string 字符串工具 ​

17:43

说明 ​

$u.strOr 按优先级返回第一个非空字符串,用于多级兜底;$u.guid 生成指定长度与字符集的随机唯一标识,默认 32 位且首字符为 u。

示例 ​

多级兜底 ​

strOr 按参数顺序返回第一个非空字符串,空串与 null 被跳过,全部为空时返回空串。

查看源码
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>

唯一标识 ​

对比 $u.guid 的默认 32 位、指定位数、firstU 关闭与 radix 为 16 的十六进制字符集,每次点击重新生成。

查看源码
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 ​

参数:

参数说明类型默认值版本
...values待取值序列,按从前到后的优先级检查any | string | null--

返回值:

类型说明
string第一个非空字符串;全部为空时返回 ''

guid ​

参数:

参数说明类型默认值版本
len生成的字符串长度number32-
firstU是否把首字符强制为 ubooleantrue-
radix字符集基数,决定可取的字符范围number--

返回值:

类型说明
string生成的随机唯一标识字符串

注意事项 ​

  • strOr 只接受非空字符串,空串、null、undefined 以及数字等非字符串值都会被跳过。
  • guid 默认生成长度 32 的字符串,并在首字符强制为 u,避免以数字开头导致 id / class 选择器非法。
  • firstU 为 false 时首字符可能为数字,不适合直接用作 id 或选择器。
  • radix 的可用范围是 2-62(字符集为 0-9A-Za-z),未传或传 0 时按 62 处理;结果由 Math.random 生成,不保证绝对唯一。