字符串工具
大约 4 分钟
这是一个字符串相关的工具类模块,提供了一些对字符串的高级操作。
stringToInt
将字符串转换为整数。
参数说明
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| str | string | '' | 要转换的字符串 |
| defaultValue | number | 0 | 转换失败时返回的默认值 |
返回值
- 类型:
number - 说明:转换后的整数,若转换失败则返回默认值。
示例
stringToInt('123'); // 123
stringToInt('abc', 10); // 10stringToBoolean
将字符串转换为布尔值。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 要转换的字符串 |
返回值
- 类型:
boolean - 说明:若字符串为
'true'或'1'(不区分大小写),则返回true,否则返回false。
示例
stringToBoolean('true'); // true
stringToBoolean('1'); // true
stringToBoolean('yes'); // falseisBlank
判断字符串是否为空或空白字符串。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | any | 要判断的值 |
返回值
- 类型:
boolean - 说明:如果值为
null、undefined、空字符串""或全为空格的字符串,则返回true,否则返回false。
示例
isBlank(null); // true;
isBlank(''); // true;
isBlank(' '); // true;
isBlank('bob'); // false;
isBlank(' bob '); // false;
isBlank(1); // false;
isBlank(0); // false;
isBlank(true); // false;
isBlank(false); // false;isNotBlank
判断字符串是否非空。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | any | 要判断的值 |
返回值
- 类型:
boolean - 说明:与 isBlank 相反,若字符串非空且非空白字符串,则返回
true。
示例
isBlank(null); // false;
isBlank(''); // false;
isBlank(' '); // false;
isBlank('bob'); // true;
isBlank(' bob '); // true;
isBlank(1); // true;
isBlank(0); // true;
isBlank(true); // true;
isBlank(false); // true;defaultIfBlank
如果字符串为空,则返回默认值。
参数说明
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| str | any | - | 要判断的值 |
| defaultStr | any | '-' | 如果为空时返回的默认值 |
返回值
- 类型:
any - 说明:如果
str是空白值,则返回defaultStr,否则返回原值。
示例
defaultIfBlank('', 'default'); // 'default'
defaultIfBlank(null, 'default'); // 'default'
defaultIfBlank('hello', 'default'); // 'hello'removeStart
删除字符串开头的指定子串。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 原始字符串 |
| remove | string | 要删除的子串 |
返回值
- 类型:
string - 说明:如果字符串以
remove开头,则返回删除后的字符串;否则返回原字符串。
示例
注
* 为任意字符
removeStart(null, *); // null
removeStart('', *); // ""
removeStart(*, null); // *
removeStart('www.domain.com', 'www.'); // "domain.com"
removeStart('domain.com', 'www.'); // "domain.com"
removeStart('www.domain.com', 'domain'); // "www.domain.com"
removeStart('abc', ''); // "abc"removeEnd
删除字符串结尾的指定子串。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 原始字符串 |
| remove | string | 要删除的子串 |
返回值
- 类型:
string - 说明:如果字符串以
remove结尾,则返回删除后的字符串;否则返回原字符串。
示例
注
* 为任意字符
removeEnd(null, *); // null
removeEnd('', *); // ""
removeEnd(*, null); // *
removeEnd('www.domain.com', '.com.'); // "www.domain.com"
removeEnd('www.domain.com', '.com'); // "www.domain"
removeEnd('www.domain.com', 'domain'); // "www.domain.com"
removeEnd('abc', ''); // "abc"appendIfMissing
如果字符串不以指定后缀结尾,则添加该后缀。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 原始字符串 |
| suffix | string | 要确保存在的后缀 |
返回值
- 类型:
string - 说明:如果字符串不以
suffix结尾,则返回加上后缀的字符串;否则返回原字符串。
示例
appendIfMissing(null, null); // null;
appendIfMissing('abc', null); // 'abc';
appendIfMissing('', 'xyz'); // 'xyz';
appendIfMissing('123', 'xyz'); // '123xyz';
appendIfMissing('123xyz', 'xyz'); // '123xyz';
appendIfMissing('abcYZ', 'yz'); // 'abcYZyz';prependIfMissing
如果字符串不以指定前缀开头,则添加该前缀。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 原始字符串 |
| prefix | string | 要确保存在的前缀 |
返回值
- 类型:
string - 说明:如果字符串不以
prefix开头,则返回加上前缀的字符串;否则返回原字符串。
示例
prependIfMissing(null, null); // null;
prependIfMissing('abc', null); // 'abc';
prependIfMissing('', 'xyz'); // 'xyz';
prependIfMissing('123', 'xyz'); // 'xyz123';
prependIfMissing('xyz123', 'xyz'); // 'xyz123';
prependIfMissing('XYZ_abc', 'xyz'); // 'xyzXYZ_abc';removeNonNumber
删除字符串中的所有非数字字符。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 要处理的原始字符串 |
返回值
- 类型:
string - 说明:返回仅包含数字字符的新字符串。
示例
removeNonNumber('abc123def456'); // '123456'
removeNonNumber('2023-04-01'); // '20230401'removeStartZero
删除字符串开头的所有连续 0 字符。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 要处理的原始字符串 |
返回值
- 类型:
string - 说明:返回删除开头连续
0后的字符串。
示例
removeStartZero('00012345'); // '12345'
removeStartZero('0abc000'); // 'abc000'removeNonNumberAndStartZero
先删除字符串中的非数字字符,再删除开头的所有 0 字符。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 要处理的原始字符串 |
返回值
- 类型:
string - 说明:返回仅包含数字,并且开头无连续
0的字符串。
示例
removeNonNumberAndStartZero('000abc123def0456xyz'); // '1230456'
removeNonNumberAndStartZero('000000'); // ''removeChinese
删除字符串中的所有中文字符。
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | string | 要处理的原始字符串 |
返回值
- 类型:
string - 说明:返回删除所有中文字符后的新字符串。
示例
removeChinese('你好123世界456'); // '123456'
removeChinese('abc中文def'); // 'abcdef'