我不太擅长 JavaScript 中的字符串操作,我想知道如何在不切断任何单词的情况下缩短字符串。我知道如何使用子字符串,但不知道如何使用 indexOf 或任何非常好的东西。
假设我有以下字符串:
text = "this is a long string I cant display"
我想将其缩减为 10 个字符,但如果它不以空格结尾,请完成该单词。我不希望字符串变量看起来像这样:
“这是我无法分辨的长字符串”
我希望它完成这个词,直到出现空格。
我不太擅长 JavaScript 中的字符串操作,我想知道如何在不切断任何单词的情况下缩短字符串。我知道如何使用子字符串,但不知道如何使用 indexOf 或任何非常好的东西。
假设我有以下字符串:
text = "this is a long string I cant display"
我想将其缩减为 10 个字符,但如果它不以空格结尾,请完成该单词。我不希望字符串变量看起来像这样:
“这是我无法分辨的长字符串”
我希望它完成这个词,直到出现空格。
如果我理解正确,您想将字符串缩短到一定长度(例如缩短"The quick brown fox jumps over the lazy dog"
到 6 个字符而不切断任何单词)。
如果是这种情况,您可以尝试以下操作:
var yourString = "The quick brown fox jumps over the lazy dog"; //replace with your string.
var maxLength = 6 // maximum number of characters to extract
//trim the string to the maximum length
var trimmedString = yourString.substr(0, maxLength);
//re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))
有很多方法可以做到这一点,但正则表达式是一种有用的单行方法:
"this is a longish string of text".replace(/^(.{11}[^\s]*).*/, "$1");
//"this is a longish"
此表达式返回前 11 个(任何)字符以及任何后续的非空格字符。
示例脚本:
<pre>
<script>
var t = "this is a longish string of text";
document.write("1: " + t.replace(/^(.{1}[^\s]*).*/, "$1") + "\n");
document.write("2: " + t.replace(/^(.{2}[^\s]*).*/, "$1") + "\n");
document.write("5: " + t.replace(/^(.{5}[^\s]*).*/, "$1") + "\n");
document.write("11: " + t.replace(/^(.{11}[^\s]*).*/, "$1") + "\n");
document.write("20: " + t.replace(/^(.{20}[^\s]*).*/, "$1") + "\n");
document.write("100: " + t.replace(/^(.{100}[^\s]*).*/, "$1") + "\n");
</script>
输出:
1: this
2: this
5: this is
11: this is a longish
20: this is a longish string
100: this is a longish string of text
我有点惊讶,对于这样一个简单的问题,有这么多难以阅读的答案,有些答案,包括所选的答案,都不起作用。
我通常希望结果字符串最多为 maxLen
字符。我也使用相同的函数来缩短 URL 中的 slug。
str.lastIndexOf(searchValue[, fromIndex])
采用第二个参数,它是在字符串中开始向后搜索的索引,使事情变得高效和简单。
// Shorten a string to less than maxLen characters without truncating words.
function shorten(str, maxLen, separator = ' ') {
if (str.length <= maxLen) return str;
return str.substr(0, str.lastIndexOf(separator, maxLen));
}
这是一个示例输出:
for (var i = 0; i < 50; i += 3)
console.log(i, shorten("The quick brown fox jumps over the lazy dog", i));
0 ""
3 "The"
6 "The"
9 "The quick"
12 "The quick"
15 "The quick brown"
18 "The quick brown"
21 "The quick brown fox"
24 "The quick brown fox"
27 "The quick brown fox jumps"
30 "The quick brown fox jumps over"
33 "The quick brown fox jumps over"
36 "The quick brown fox jumps over the"
39 "The quick brown fox jumps over the lazy"
42 "The quick brown fox jumps over the lazy"
45 "The quick brown fox jumps over the lazy dog"
48 "The quick brown fox jumps over the lazy dog"
对于蛞蝓:
for (var i = 0; i < 50; i += 10)
console.log(i, shorten("the-quick-brown-fox-jumps-over-the-lazy-dog", i, '-'));
0 ""
10 "the-quick"
20 "the-quick-brown-fox"
30 "the-quick-brown-fox-jumps-over"
40 "the-quick-brown-fox-jumps-over-the-lazy"
每个人似乎都忘记了 indexOf 需要两个参数——要匹配的字符串和开始查找的字符索引。您可以在 10 个字符后的第一个空格处断开字符串。
function cutString(s, n){
var cut= s.indexOf(' ', n);
if(cut== -1) return s;
return s.substring(0, cut)
}
var s= "this is a long string i cant display";
cutString(s, 10)
/* returned value: (String)
this is a long
*/
Lodash 有一个专门为此编写的函数: _.truncate
const truncate = _.truncate
const str = 'The quick brown fox jumps over the lazy dog'
truncate(str, {
length: 30, // maximum 30 characters
separator: /,?\.* +/ // separate by spaces, including preceding commas and periods
})
// 'The quick brown fox jumps...'