JavaScript Program to Truncate a String to a Certain Length Last Updated : 10 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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 methodUsing JavaScript String substring method Using Regular ExpressionUsing JavaScript String padEnd methodUsing JavaScript String slice methodThis 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)); Outputgfg is a good place Using JavaScript String substring methodThis 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)); Outputgfg is a good place Using Regular ExpressionUsing 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" OutputHello Using JavaScript String padEnd methodThis 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)); Outputgfg is a good place Comment More infoAdvertise with us Next Article JavaScript Program to Truncate a String to a Certain Length A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League javascript-string JavaScript-DSA JavaScript-Program Geeks Premier League 2023 +3 More Similar Reads JavaScript Program to Truncate a String to a Certain Length and Add Ellipsis (...) In this article, we will see how to truncate a string to a certain length and add an ellipsis (...) in JavaScript. To truncate a string to a specific length and add an ellipsis (...) in JavaScript means shortening the string to a predefined character count, indicating that the content continues beyo 3 min read JavaScript Program for Generating a String of Specific Length In this article, we are going to discuss how can we generate a string having a specific length. You might encounter many situations when working with JavaScript where you need to produce a string with a specified length. This could be done for a number of reasons, including generating passwords, uni 4 min read JavaScript Program to Remove Last Character from the String In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string. Example: Input : Geeks for geeks Output : Geeks for geek Input : Geeksforgeeks Output : 3 min read JavaScript - How to Get the Last N Characters of a String? Here are the different methods to get the last N characters of a string in JavaScript.1. Using slice() MethodThe slice() method is the most commonly used approach for extracting the last N characters, thanks to its simplicity and support for negative indices.JavaScriptconst getChar = (s, n) => s. 2 min read JavaScript- Remove Last Characters from JS String These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last charact 2 min read Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a 4 min read JavaScript - How to Pad a String to Get the Determined Length? Here are different ways to pad a stirng to get the specified length in JavaScript.1. Using padStart() MethodThe padStart() method can be used to pad a string with the specified characters to the specified length. It takes two parameters, the target length, and the string to be replaced with. If a nu 3 min read Java Program to Add Characters to a String We will be discussing out how to add character to a string at particular position in a string in java. It can be interpreted as follows as depicted in the illustration what we are trying to do which is as follows: Illustration: Input: Input custom string = HelloOutput: --> String to be added 'Gee 4 min read How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210 2 min read JavaScript String trimStart() and trimLeft() Method This article demonstrates trimEnd and trimRight methods of JavaScript string. Both methods are used to perform the same task and their implementation is shown below: JavaScript trimStart() method:The trimStart() method in JavaScript is used to remove whitespace from the beginning of a string. The va 2 min read Like