Display only 10 characters of a long string?

Viewed 279149

How do I get a long text string (like a querystring) to display a maximum of 10 characters, using JQuery?

Sorry guys I'm a novice at JavaScript & JQuery :S
Any help would be greatly appreciated.

14 Answers
var example = "I am too long string";
var result;

// Slice is JS function
result = example.slice(0, 10)+'...'; //if you need dots after the string you can add

Result variable contains "I am too l..."

Although this won't limit the string to exactly 10 characters, why not let the browser do the work for you with CSS:

.no-overflow {
    white-space: no-wrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

and then for the table cell that contains the string add the above class and set the maximum permitted width. The result should end up looking better than anything done based on measuring the string length.

A = "a lennnnnnnnnnnnngthy string ";

word = A.substring(0, number_of_words_to_appear) + "...";

console.log(word)

const text = 'imathelongtextblablabla';
const showLess = false;
{!showLess && ${text.substring(0, 10)}}

{!showLess && "..."}

Related