Skip to content

字符串工具 API

默认导出为 StringUtils

使用指引(做什么、何时用)

  • UI 显示:ellipsisStr 在长串中间省略,适合订单号/地址/密钥显示
  • 命名转换:toCamelCasesnake-case/kebab-case 转为 camelCase
  • 随机/颜色:createRandomStr/randomHexColor 用于占位/测试/主题预览
  • 基本处理:reverseString/countOccurrences 用于简单文本操作

导入

ts
import { StringUtils } from 'nex-lib';

randomHexColor(): string

ts
StringUtils.randomHexColor(); // '#a1b2c3'

toCamelCase(str: string): string

ts
StringUtils.toCamelCase('hello_world'); // 'helloWorld'
StringUtils.toCamelCase('foo-bar');     // 'fooBar'

reverseString(str: string): string

ts
StringUtils.reverseString('abc'); // 'cba'

ellipsisStr(str: string, start = 6, end = 4): string

ts
StringUtils.ellipsisStr('abcdefghijklmn', 6, 4); // 'abcdef...jklmn'

createRandomStr(len = 6): string

ts
StringUtils.createRandomStr(8); // 随机字符串

countOccurrences(str: string, word: string): number

ts
StringUtils.countOccurrences('foofoo', 'foo'); // 2

Released under the ISC License.