ArkTS 测试数据生成利器:getRandomStr 和 getRandomChinese 实战指南 文章目录背景总览方法getRandomChineseChar / getRandomChinese随机汉字getRandomStr自定义字符池随机字符串getRandomColor随机十六进制颜色完整示例生成随机用户信息写在最后背景近期发现一款很有意思的HarmonyOS 三方库, 地址 pura/harmony-utils(V1.4.0) , 作者是桃花镇童长老, 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦案例demo导航展示↓↓↓↓↓↓接下来言归正传 ↓↓↓↓上一篇讲了数字类随机方法这篇来讲RandomUtil里更有意思的三类随机汉字生成随机中文字符随机字符串自定义字符池生成字符串随机颜色生成随机十六进制颜色值总览方法先来看下这个三方库的方法有哪些getRandomChineseChar / getRandomChinese随机汉字getRandomChineseChar()生成单个随机汉字getRandomChinese(n)生成 n 个汉字组成的字符串this.Btn(getRandomChineseChar() 单个汉字,#E74C3C,(){constvRandomUtil.getRandomChineseChar();this.addLog(getRandomChineseChar() → ${v});})Row({space:8}){Text(长度:).fontSize(13).fontColor(#666)TextInput({text:this.chineseCount.toString()}).layoutWeight(1).height(38).fontSize(13).type(InputType.Number).onChange(v{this.chineseCountparseInt(v)||4;})}this.Btn(getRandomChinese(${this.chineseCount}),#E74C3C,(){constvRandomUtil.getRandomChinese(this.chineseCount);this.addLog(getRandomChinese(${this.chineseCount}) → ${v});})实际运行结果getRandomChineseChar()→ 每次返回一个随机汉字比如驿、梓、铃getRandomChinese(4)→ 4个随机汉字比如幻轩青瓷生成的汉字来自常用汉字范围CJK 统一汉字Unicode\u4e00到\u9fa5共 20902 个汉字。不会生成生僻字适合生成测试用的中文内容。使用场景生成随机的中文昵称可以配合数字UI 测试时填充中文占位内容生成随机的中文标题测试排版getRandomStr自定义字符池随机字符串getRandomStr(length, strPool)从你指定的字符池里随机抽取字符生成指定长度的字符串Row({space:8}){Text(长度:).fontSize(13).fontColor(#666)TextInput({text:this.strLength.toString()}).layoutWeight(1).height(38).fontSize(13).type(InputType.Number).onChange(v{this.strLengthparseInt(v)||8;})}this.Btn(getRandomStr(${this.strLength}, 数字池),#27AE60,(){constvRandomUtil.getRandomStr(this.strLength,0123456789);this.addLog(getRandomStr(${this.strLength}, 0-9) → ${v});})this.Btn(getRandomStr(${this.strLength}, 字母池),#27AE60,(){constvRandomUtil.getRandomStr(this.strLength,abcdefghijklmnopqrstuvwxyz);this.addLog(getRandomStr(${this.strLength}, a-z) → ${v});})this.Btn(getRandomStr(${this.strLength}, 字母数字混合池),#27AE60,(){constvRandomUtil.getRandomStr(this.strLength,ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789);this.addLog(getRandomStr(${this.strLength}, alphanumeric) → ${v});})实际运行结果length8 时数字池34719826这样的纯数字字符串字母池kxmvtpwj这样的纯小写字母字母数字混合aB3kZ7xM这样的大小写字母加数字混合三个典型场景场景1 - 生成短信验证码纯数字6位constsmsCodeRandomUtil.getRandomStr(6,0123456789);// 例如847291场景2 - 生成邀请码大写字母数字8位constinviteCodeRandomUtil.getRandomStr(8,ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789);// 例如K7P3NZ2A场景3 - 生成临时密码字母数字混合12位consttempPasswordRandomUtil.getRandomStr(12,ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789);// 例如kzP7nXmQ3rBvgetRandomColor随机十六进制颜色生成随机的 CSS 十六进制颜色值#RRGGBB格式Row({space:12}){Button(getRandomColor()).layoutWeight(1).height(40).fontSize(14).fontColor(#fff).backgroundColor(#F39C12).borderRadius(8).onClick((){constcRandomUtil.getRandomColor();this.randomColorc;this.addLog(getRandomColor() → ${c});})Row().width(40).height(40).backgroundColor(this.randomColor).borderRadius(8).border({width:1,color:#ddd})}实际运行结果getRandomColor()→ 类似#3A7FD5这样的随机颜色值this.randomColor存的就是颜色字符串直接传给backgroundColor属性就能用ArkTS 支持直接用十六进制字符串设置颜色。使用场景生成随机头像背景色数据可视化里给不同系列分配颜色UI 主题测试完整示例生成随机用户信息把以上方法组合起来生成一套随机测试用户数据import{RandomUtil}from../Utils/RandomUtil;functiongenerateRandomUser(){constnameRandomUtil.getRandomChinese(2)RandomUtil.getRandomChinese(1);// 2-3个字姓名constinviteCodeRandomUtil.getRandomStr(8,ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789);constavatarColorRandomUtil.getRandomColor();constageRandomUtil.getRandomInt(18,60);return{name,inviteCode,avatarColor,age};}// 调用示例constusergenerateRandomUser();// 结果类似// { name: 林青云, inviteCode: K3NP7ZA2, avatarColor: #5E8BC7, age: 32 }写在最后随机字符串的关键是字符池设计——根据你的业务需求选合适的字符集。验证码用数字池邀请码用字母数字密码用更丰富的字符集。getRandomColor直接返回可用的颜色字符串在 ArkTS 里赋值给颜色属性无需任何转换用起来特别顺手。下一篇讲RandomUtil的 UUID 生成——包括 36 位标准格式、32 位无横杠格式、以及二进制 UUID。