JavaScript Program to Truncate a String to a Certain Length

In this article, we are going to learn how can we truncate a string to a certain length. Truncating a string to a certain length in JavaScript means shortening a string so that it doesn’t exceed a specified maximum length. This can be useful for displaying text in user interfaces, such as titles or descriptions while ensuring that the text does not exceed a certain number of characters.

Table of Content

  • Using JavaScript String slice method
  • Using JavaScript String substring method
  • Using Regular Expression
  • Using JavaScript String padEnd method

Using JavaScript String slice method

This approach uses the slice() method to extract a portion of the string up to the desired length.

Example: This example shows the use of the above-explained approach.

Javascript
// JavaScript function for 
// getting a string of a 
// specific length
function gfgFun(str, MaxLength) {
    if (str.length > MaxLength) {
        return str.slice(0, MaxLength);
    } else {
        return str;
    }
}

// Input string
const str = "gfg is a good place for growth";

// Desired length of a string
const MaxLength = 20;
console.log(gfgFun(str, MaxLength));

Output
gfg is a good place 

Using JavaScript String substring method

This approach uses the substring() method to get a substring up to the specified length.

Example: This example shows the use of the above-explained approach.

Javascript
// JavaScript function for 
// getting a string of a 
// specific length
function gfgFun(str, maxLength) {
    if (str.length > maxLength) {
        return str.substring(0, maxLength);
    } else {
        return str;
    }
}

// Input String
const str = "gfg is a good place for growth";

// Desired length of a string
const maxLength = 20;
console.log(gfgFun(str, maxLength));

Output
gfg is a good place 

Using Regular Expression

Using regular expression to truncate a string involves creating a regex pattern matching the desired length, then extracting the matched substring. The pattern matches any character zero to the desired length times.

Example:

JavaScript
function truncateString(str, maxLength) {
  const regex = new RegExp(`^.{0,${maxLength}}`);
  return str.match(regex)[0];
}

const inputString = "Hello, world!";
const truncatedString = truncateString(inputString, 5);
console.log(truncatedString); // Output: "Hello"

Output
Hello

Using JavaScript String padEnd method

This approach uses the padEnd method to truncate a string to the specified length. The padEnd method pads the current string with another string until the resulting string reaches the given length. However, we can use this method to achieve truncation by padding with an empty string and then slicing the result.

Example:

JavaScript
// JavaScript function for 
// truncating a string to a 
// specific length using padEnd
function truncateString(str, maxLength) {
    return str.padEnd(maxLength).slice(0, maxLength);
}

// Input string
const str = "gfg is a good place for growth";

// Desired length of a string
const maxLength = 20;
console.log(truncateString(str, maxLength));

Output
gfg is a good place