showing results for - "truncate a string"
Emil
07 Sep 2016
1var string = "ABCDEFG";
2var truncString = string.substring(0, 3); //Only keep the first 3 characters
3console.log(truncString); //output: "ABC"
Micaela
31 Jun 2018
1const truncateString = (str, num) => {
2  
3  /*******  Method One(1)  *******/
4      // Return the actual string if num is equal or greater than str, 
5      // else return the slice/copy the str from index 0 to index n(num), then truncate. 
6    return num >= str.length ? str : str.slice(0, num)+'...'
7    
8  /******* Method Two(2) *******/
9    let newString = ''
10    	// Return same string if number length is greater or equal
11    if(num >= str.length) newString += str; 
12    	// Split each string, set new string to the spliced arr, join the new string and truncate
13    else newString += str.split('').splice(0, num).join('')+'...'
14
15    return newString
16  
17  /******* Method Three(3) *******/
18  let truncateString = ''
19  	// Optimize, to aviod looping through str when it is greater than the num
20  if(num >= str.length) {
21    truncateString = str
22    return truncateString
23    }
24  else for(let i = 0; i < num; i++) truncateString += str[i]
25  return truncateString+'...'
26  	
27  	// OR
28  
29  let truncateString = ''
30  for(let i = 0; i < num; i++) truncateString += str[i]
31  	// Conditionally search to avoid result returning undefined for num greater than str length
32  return num >= str.length ? str : truncateString+'...'
33  
34}
35truncateString("Peter Piper picked a peck of pickled peppers", 11);
María
08 Mar 2016
1function truncateString(str, num) {
2  // Clear out that junk in your trunk
3  if (str.length > num) {
4    return str.slice(0, num) + "...";
5  } else {
6    return str;
7  }
8}
9
Irene
09 Jan 2020
1function truncateString(str, num) {
2 
3  if (str.length > num) {
4    return str.slice(0, num) + "...";
5  } else {
6    return str;
7  }
8}
9
10truncateString("Lorem Ipsum placeholder text in any number of characters, words sentences or paragraphs", 9)  // returns Lorem Ips...
11
queries leading to this page
str truncate is not a functiontruncate a string in nodejstruncatestring using jshow to truncate strings javascriptjs truncate every 2 charshow to truncate string in javascript how to shorten a string jstruncate meaning in javascript stringtruncate string truncate string nodejshow to truncate string jstruncate a string in jstruncate content using jstruncate stringstruncate stringjavascripthow to shorten a string in node jstruncate javascript stringtruncated jstruncate string hstruncate string javasripttruncate the string in javascriptshorten a string with javascripttruncate string at character javascriptnode truncate stringjavascript truncate string full wordjs why does 23 character truncate stringstring truncate javascripthow to truncate a string in jshow to shorten a string javascriptwhat is truncating a string in javascriptstring truncatetruncate strings jsjavascript text truncatejavascript string truncate methodhow to truncate strings in jstruncate strnig jshow to truncate text in jstruncate a stringhow to truncate string for 4 numbers javascripttruncate characters in javascripttruncating a stringjavascript string truncatetruncating stringhow to truncate in a stringhow to reduce the length of a string in javascriptfunction in js to trunc ate string and add javasscript truncate stringjavascript truncate to 50 charstruncate method in javascripttruncate string from starting of particular word javascriptshorten a string javascripttruncate a stringi n jsjavascript truncate string with truncate a string meaningtruncate stringjavascript truncate charecters from stringjs truncate string from string to stringjs truncate textjs text truncatestring truncate jstruncate jshow to truncate a paragraph in javascriptjs truncates the contenttruncate javascripttruncate js functiontruncate string javascript fccjavscript truncate text stringtruncate 3a function 28str 2c len 29how to truncate a string javascripttruncate strign jstruncated data javascriptjs string truncatetruncate string in jstruncate substringjs truncate stringjs truncate text stringtruncate string javascript javawhat to call text truncated in javascripttruncate an string javascriptjs truncate string javascript truncate stringtruncate string using javascripthow to truncate text javascripttruncate string in nodejsjavascript method for truncating stringjs 23 character truncates stringtruncate string jstruncate words jstruncate string using jstrunctate jsjavascript shorten stringtruncate text in javascripttruncate string sjvascripttruncate string with javascripttruncate a string javascripttruncate particular string from string javascripttruncate string in javascriptjavascript return truncate stringtruncate javascript codehow to short string in javascripttruncate string javascript mediumtruncate javascript string functionhow to truncate to 100 in expresstruncate a string jstruncate a string straight javascriptstringtruncate jshow to truncate a string in javascript using slicehow to truncate a string from another stringtruncate in javascript stringjs trucate stringstring trunk javascripttruncate a string using javascriptjavascript concatenate stringhow to truncate content in javascriptjs shorten a strnigtruncate after number of character javascripttruncate string javascriptstring truncate javascript javascript function to truncate a stringtruncate and untruncate string javascriptjavascript method truncate stringtruncate paragraph javascripttruncate characters javascriptjavascirpt truncate stringstring truncate in javascripttruncate strings using javascripttruncate strig javascriptwhat is mean to truncate a stringjs how to truncate stringshorten string by one javascripttruncate name javascripttruncate string in js and return tranculate string javascriptjavascript beutifiestruncate text with javascripttruncate a dtring jshow to truncate text with jsstr truncate jstruncat a js stringhow to find and truncate a character from a string in javascriptjavascript truncate a number of characterstruncate string 2b javascripttruncate string injshow to truncate to 100 characters in javascripttruncate method jstruncate text javascriptwrite a javascript function to truncate a string to a certain number of wordshow to truncate a string in javascripthow to truncate string in jsjavascript truncating stringhow to truncade a variable in javascriptjs truncatetruncating string in javascriptshorten string jsstring truncate meansstring shorten in javascripttruncate string from string javascriptjavascript truncate string htmljavascript truncatetruncate str length in jstruncating a string typescripttruncating a string in javascripttext truncate javascriptjavascript truncate texttruncate a string in javascriptjs add truncate stringjavascript truncate string methodhow to truncate a stringjavascript truncate string to lengthhow to truncate text in javascripttruncate string and add javascripttruncate string meanstruncate text jshow to truncate characters in javascripttruncate a string