0

In PHP we have functions to cut long texts, to make a kind of 'preview' by stoping the whole result with the subrt() function in order to put a limit to the string.

Is there any way of do the same but in JavaScript?

what I need is to show

blablablabla...

instead of blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla (the whole text) when showing results {{mytext}}

1
  • 1
    You might also be interested in using simple CSS rules, like text-overflow.
    – ghybs
    Jul 19, 2017 at 17:46

2 Answers 2

0

This is a relatively straight forward function to write in javascript. Substring is a function in javascript with the signature str.substring(startIndex, [endIndex]). So if you wanted to take the first n characters from a string and then append an ellipses you could do the following

// verbose
function shorten(str, n) {
    var chopped = str.substr(0, n);
    var formatted = chopped + '...';
    return formatted;

}
// one liner
function shorten(str, n) {
    return str.substr(0, n) + '...';
}
// shorten('foobarbatbaz', 3) -> 'foo...'
-1

I don't know about meteor, but javscript itself has a substr function for strings, <string>.substr(<length>) for instance "asdf123123123123123".substr(4) gives you "asdf" EDIT: it gives you "123123123123123", I was thinking of substr(0,4)

1
  • Almost. That would give you 123123123123123. Did you mean .substr(0,4)?
    – Skam
    Jul 19, 2017 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.