JavaScript - How to Pad a String to Get the Determined Length? Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 number is to be padded, it has to be first converted into a string by passing it to the String constructor, then the padStart() method can be used on the string. SyntaxString(strToPad).padStart(padLength, padChar) JavaScript str = "abcdefg"; // Padded string output = String(example1).padStart(10, '*'); // Display output console.log(output); Outputabcdefg 1234 ***abcdefg ^#^#^#12342. Using repeat() and slice() MethodsThe string.repeat() is an inbuilt method in JavaScript that is used to build a new string containing a specified number of copies of the string on which this method has been called. JavaScript function padStr(padChar, padLength, originalString) { // Convert the pad character and original // string to String padChar = String(padChar); originalString = String(originalString); // Calculate the length of padding characters padLeft = padLength - originalString.length; // Create the pad string padString = padChar.repeat(padLeft); // Add the pad string to the original string // slice it to the padLength if it exceeds // the pad length specified newString = (padString + originalString).slice(-padLength); return newString; } str = "abcdefg"; // Display input console.log(str); // Padded string output = padStr('*', 10, str); // Display output console.log(output); Outputabcdefg 1234 ***abcdefg ^#^#^#12343. Using JavaScript LoopA while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntaxwhile (boolean condition) { loop statements...} JavaScript function padStr(padChar, padLength, originalString) { newString = originalString; // Loop until desired length is reached while (newString.length < padLength) { // Add padChar each time paddedString = padChar + paddedString; } return newString; } // Input string str = "abcdefg"; console.log(str); // Padded string output = String(str).padStart(10, '*'); // Display output console.log(output); Outputabcdefg 1234 ***abcdefg ^#^#^#12344. Using padEnd() MethodThe padEnd() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the end (right) of the current string. Similar to padStart(), it takes two parameters: the target length and the pad string. JavaScript function pad() { // Input string let example1 = "abcdefg"; let example2 = 1234; // Display input string console.log(example1); console.log(example2); // Padded string let appended_out = String(example1).padEnd(10, '*'); let appended_out2 = String(example2).padEnd(10, '^#'); // Display output console.log(appended_out); console.log(appended_out2); } // Function Call pad(); Outputabcdefg 1234 abcdefg*** 1234^#^#^# Comment More infoAdvertise with us Next Article JavaScript - How to Pad a String to Get the Determined Length? sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA Similar Reads Create a string with multiple spaces in JavaScript We have a string with extra spaces and if we want to display it in the browser then extra spaces will not be displayed. Adding the number of spaces to the string can be done in the following ways. In this article, we are going to learn how to Create a string with multiple spaces in JavaScript. Below 3 min read How to define a new method for calculating the actual length of string in JavaScript using prototype ? In JavaScript, you can use the String. length property to get the length of a string variable. This method will give you a string that is enclosed in quotes no matter what it is. We will get one issue while using this String. length method. Let's discuss when we will get the issue with that String.l 3 min read JavaScript string.length The string.length is a property in JS which is used to find the length of a given string. The string.length property returns 0 if the string is empty.javascriptlet s1 = 'gfg'; console.log(s1.length); let s2 = ''; console.log(s2.length); let s3 = '#$%A'; console.log(s3.length);Output3 0 4 Syntax:stri 1 min read JavaScript String length Property The JavaScript length property is a fundamental feature used to determine the number of characters in a string. It is essential for various string operations, such as validation, manipulation, and iteration. Understanding how to effectively use the length property can simplify tasks like checking in 2 min read JavaScript String padEnd() Method The padEnd() method in JavaScript is used to pad a string with another string until it reaches the given length. The padding is applied from the right end of the string.Syntax:string.padEnd( targetLength, padString );Parameters: This method accepts two parameters as mentioned above and described bel 3 min read Java String Class indent() Method With Examples JDK 12 introduced indent() method in Java.lang.String class. This method is useful to add or remove white spaces from the beginning of the line to adjust indentation for each string line. Syntax: public String indent(int n) Parameter: It takes integer n as input and does indentation accordingly. Al 3 min read JavaScript String padStart() Method The padStart() method in JavaScript is used to pad a string with another string until it reaches the given length. The padding is applied from the left end of the string. Syntax:string.padStart(targetLength, padString)Parameters:This method accepts two parameters as mentioned above and described bel 3 min read Can we Change the Length of a JS String using Length Property? The length property of a string returns the number of characters in the string, including spaces, punctuation, and special characters. It is a read-only property, which means that you cannot assign a new value to length directly to change the string's size. The given below example shows that you can 1 min read JavaScript String Methods JavaScript strings are the sequence of characters. They are treated as Primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing. The following are methods that we can call on strings.slice() extra 11 min read Batch Script - String length In this article , we are going to learn how to find length of any String using Batch Script. Batch Script :@echo off set str=Geeks For Geeks call :strLen str strlen echo String is %strlen% characters long pause exit /b :strLen setlocal enabledelayedexpansion :strLen_Loop if not "!%1:~%len%!"=="" set 2 min read Like