cutString(string, cutFrom) {
string = string.split('');
if (string.length > cutFrom) {
for (let i = 0; i < string.length; i++) {
if (i === cutFrom - 1) {
string[i] = '...';
} else if (i > cutFrom - 1) {
string[i] = '';
}
}
}
return string.join('');
}
String.prototype.ellipsis = function(n){
return this.slice(0,n)+'...';
};
var my_str = 'Моя длинная строка';
console.log( my_str.ellipsis(8) ); // "Моя длин..."
// Метод скоращения строки
// Показывает последние n-исмовлов, остальное заменяет на ...
String.prototype.shortStr = function(n) {
const dots = '...'
let string = this.toString()
if ( string.length <= n ){
return this
} else {
return dots + string.substring(string.length - n)
}
}