admin管理员组文章数量:1310441
I want to truncate a string after certain characters length in javascript. When the character length is reached then string should not be cut in the middle of the word rather it should plete the word and then truncate the string. What I have tried uptil now cuts the string before the cutting word. I want to include the cutting word in returned string. Here is my code:
function truncateString(yourString, maxLength) {
var trimmedString = yourString.substr(0, maxLength);
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")));
return trimmedString;
}
now when I call this function on with following parameters:
truncateString('The quick brown fox jumps over the lazy dog',6)
The output is 'The' rather than 'The quick
.
Please point out what I need to change. Thanks
I want to truncate a string after certain characters length in javascript. When the character length is reached then string should not be cut in the middle of the word rather it should plete the word and then truncate the string. What I have tried uptil now cuts the string before the cutting word. I want to include the cutting word in returned string. Here is my code:
function truncateString(yourString, maxLength) {
var trimmedString = yourString.substr(0, maxLength);
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")));
return trimmedString;
}
now when I call this function on with following parameters:
truncateString('The quick brown fox jumps over the lazy dog',6)
The output is 'The' rather than 'The quick
.
Please point out what I need to change. Thanks
Share Improve this question edited Mar 8, 2019 at 18:42 Nafees Anwar 6,6083 gold badges27 silver badges44 bronze badges asked Mar 8, 2019 at 18:31 user2032090user2032090 971 gold badge2 silver badges7 bronze badges 2- 3 I love the great variety of reasonable answers already posted! – Scott Sauyet Commented Mar 8, 2019 at 18:45
- According to a quick test, index of is way faster than regex codepen.io/ArthyFiciel/pen/pYPMgL – Arthur Commented Mar 8, 2019 at 19:11
5 Answers
Reset to default 6You can search for the index of immediate space after the maxLength
by using the second parameter of indexOf
function truncateString(yourString, maxLength) {
// get the index of space after maxLength
const index = yourString.indexOf(" ", maxLength);
return index === -1 ? yourString : yourString.substring(0, index)
}
const str = 'The quick brown fox jumps over the lazy dog';
console.log(truncateString(str,6))
console.log(truncateString(str,10))
console.log(truncateString(str,100))
One alternative is using regex.
You can build a regex pattern based on the value passed to function.
^.{${value}}.*?\\b
| |_____________ expression to get value upto next word boundry
|
|___________________ Value passed to function
let trmStr = (input,value) => {
let reg = `^.{${value}}.*?\\b`
let regex = new RegExp(reg)
return input.match(regex)
}
console.log(trmStr('The quick brown fox jumps over the lazy dog', 6))
So long as the maxLength is on a non-white space character, increase it.
function truncateString(yourString, maxLength) {
while (maxLength < yourString.length && yourString[maxLength] != ' '){
maxLength++;
}
return yourString.substr(0, maxLength);
}
console.log(
truncateString('The quick brown fox jumps over the lazy dog',6)
)
In your example:
trimmedString // "The qu"
trimmedString.length // 6
trimmedString.lastIndexOf(" ") // 3
Math.min(trimmedString.length, trimmedString.lastIndexOf(" ") // 3
So currently you go to the space that occurred before the current word, instead of the space after it.
Here's a potential solution:
- Find the
endIndex
by finding the index of the first space (" "
) that occurs on or after themaxLength
(seeindexOf
) - Return the substring ending just before
endIndex
You can doing it with reduce function. Add a word while total lenght isn't reach.
function truncateString(yourString,maxLength)
{
return yourString.split(' ').reduce((acc, str) => { return (acc.length < maxLength) ? acc + " " + str : acc }, '')
}
console.log(truncateString('The quick brown fox jumps over the lazy dog',6))
本文标签: Truncate string after the cutting word in JavascriptStack Overflow
版权声明:本文标题:Truncate string after the cutting word in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741831434a2399955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论