任何人都有一个方便的方法来截断中间的字符串?就像是:
truncate ('abcdefghi', 8);
Run Code Online (Sandbox Code Playgroud)
会导致
'abc...hi'
Run Code Online (Sandbox Code Playgroud)
更新:
更完整一点
mVC*_*Chr 21
这是一种方法来切断字符串substr:
var truncate = function (fullStr, strLen, separator) {
if (fullStr.length <= strLen) return fullStr;
separator = separator || '...';
var sepLen = separator.length,
charsToShow = strLen - sepLen,
frontChars = Math.ceil(charsToShow/2),
backChars = Math.floor(charsToShow/2);
return fullStr.substr(0, frontChars) +
separator +
fullStr.substr(fullStr.length - backChars);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7488 次 |
| 最近记录: |