Module: string

本模块提供字符串处理相关方法。
Source:

Methods

(static) compareVersions(versionA, versionB) → {number}

版本号对比。
Parameters:
Name Type Description
versionA string 待比较版本 A。
versionB string 待比较版本 B。
Since:
  • 1.6.0
Source:
Returns:
大于 0 时,表示版本 A 大于版本 B; 小于 0 时,表示版本 B 大于版本 A; 等于 0 时,表示两个版本号一致。
Type
number

(static) cutStr(str, length, optionsopt) → {string}

如果目标字符串超出限制长度,则进行截断并拼接省略符号;否则返回目标字符串。
Parameters:
Name Type Attributes Description
str string 目标字符串。
length number 限制的长度。
options Object <optional>
选项。
Properties
Name Type Attributes Default Description
mode number <optional>
2 非英文字符的单位长度。已废弃,请使用 nonEnLen。
enLen number <optional>
1 英文字符的单位长度。
nonEnLen number <optional>
2 非英文字符的单位长度。
ellipsis string <optional>
'...' 省略符号。
Source:
Returns:
截断后的字符串。
Type
string
Example
cutStr('测试一下', 5); // '测试...'
cutStr('测试一下', 8); // '测试一下'
curStr('1测试2测试3', 3.5, { enLen: 0.5, nonEnLen: 1 }); // 1测...

(static) escapeHTML(str) → {string}

把指定字符串中的 HTML 预留字符替换成 HTML 实体。
Parameters:
Name Type Description
str string 指定字符串。
Source:
Returns:
替换后的字符串。
Type
string

(static) nl2br(str) → {string}

把指定字符串中的换行符替换成 <br />。
Parameters:
Name Type Description
str string 指定字符串。
Source:
Returns:
替换后的字符串。
Type
string

(static) randomStr(length, prefixopt) → {string}

生成随机字符串。
Parameters:
Name Type Attributes Description
length number 字符串长度。
prefix string <optional>
字符串前缀(不计入长度)。
Since:
  • 1.6.0
Source:
Returns:
生成的随机字符串。
Type
string

(static) removeTags(str) → {string}

移除指定字符串中的 HTML 标签。
Parameters:
Name Type Description
str string 指定字符串。
Source:
Returns:
处理后的字符串。
Type
string

(static) strLen(str, optionsopt) → {number}

计算字符串长度(可分别指定英文字符和非英文字符的单位长度)。
Parameters:
Name Type Attributes Description
str string 字符串。
options number | Object <optional>
为数字时表示非英文字符单位长度(此时英文字符单位长度为 1);为 Object 时表示选项。
Properties
Name Type Attributes Default Description
enLen number <optional>
1 英文字符单位长度。
nonEnLen number <optional>
2 非英文字符单位长度。
Source:
Returns:
字符串长度。
Type
number
Example
strLen('abcde;'); // 6
strLen('abc测试;'); // 9
strLen('abc测试;', { enLen: 0.5, nonEnLen: 1 }); // 4.5