Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript truncate string

var string = "ABCDEFG";
var truncString = string.substring(0, 3); //Only keep the first 3 characters
console.log(truncString); //output: "ABC"
Comment

how to truncate string in javascript

function truncateString(str, num) {
  if (str.length > num) {
    return str.slice(0, num) + "...";
  } else {
    return str;
  }
}
Comment

javascript truncate string full word

const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;

// Example
truncate('This is a long message', 20, '...'); 
Comment

PREVIOUS NEXT
Code Example
Javascript :: drupal 8 get node from path alias 
Javascript :: typescript filter array of objects 
Javascript :: get column from 2D array javascript 
Javascript :: javascript print out 
Javascript :: longest word javascript 
Javascript :: react native class component constructor 
Javascript :: disable text selection in js 
Javascript :: mouseover javascript 
Javascript :: compress string javascript 
Javascript :: get random numbers javascript 
Javascript :: react native open phone 
Javascript :: js date subtract days 
Javascript :: express search query template 
Javascript :: js check if obj all false 
Javascript :: get request react 
Javascript :: next js navigation to other page in a function 
Javascript :: validate age javascript 
Javascript :: vue test form input 
Javascript :: make image circle css react 
Javascript :: append after element jquery 
Javascript :: google sign up react npm 
Javascript :: javascript to help find overflow elements 
Javascript :: javascript get keycode from char 
Javascript :: jquery disable keypress 
Javascript :: How to make "select option" select option for quantity 
Javascript :: get all div elements javascript 
Javascript :: js alphabets array 
Javascript :: nested array of object shows as object in console log output js 
Javascript :: today in moment 
Javascript :: react open url with button 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =