Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to pad a string to get the determined length using JavaScript...

How to pad a string to get the determined length using JavaScript ?

In this article, we will pad a string to get the determined length using JavaScript. To achieve this we have a few methods in javascript which are described below:

Methods to pad a string to get the determined length:

Method 1: Using the padStart() method.

The 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. The target length is the length of the resulting string after the current string has been padded. The second parameter is the characters that the string would be padded. 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 is used on this string. 

Syntax:

String(strToPad).padStart(padLength, padChar)

Example: This example uses the above approach to pad a string.

Javascript




function pad() {
    // Input string
    example1 = "abcdefg";
    example2 = 1234;
     
    // Display input string
    console.log(example1);
    console.log(example2);
 
    // Padded string
    prepended_out = String(example1).padStart(10, '*');
    prepended_out2 = String(example2).padStart(10, '^#');
     
    // Display output
    console.log(prepended_out);
    console.log(prepended_out2);
}
 
// Function Cal
pad();


Output

abcdefg
1234
***abcdefg
^#^#^#1234

Method 2: Custom function using repeat() and slice() method.

The 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.

Syntax:

string.repeat(count);

The Javascript arr.slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged.

Syntax:

arr.slice(begin, end)

Example: This example uses the above approach to pad a string.

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;
}
 
function pad() {
    // Input string
    example1 = "abcdefg";
    example2 = 1234;
     
    // Display input
    console.log(example1);
    console.log(example2);
 
    // Padded string
    prepended_out = padStr('*', 10, example1);
    prepended_out2 = padStr('^#', 10, example2);
     
    // Display output
    console.log(prepended_out);
    console.log(prepended_out2);
}
 
// Function Cal
pad();


Output

abcdefg
1234
***abcdefg
^#^#^#1234

Method 3: Using JavaScript loops

A 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. 

Syntax :

while (boolean condition) {
loop statements...
}

Example:

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;
}
 
function pad() {
    // Input string
    example1 = "abcdefg";
    example2 = 1234;
    console.log(example1);
    console.log(example2);
 
    // Padded string
    prepended_out = String(example1).padStart(10, '*');
    prepended_out2 = String(example2).padStart(10, '^#');
     
    // Display output
    console.log(prepended_out);
    console.log(prepended_out2);
}
 
// Function Cal
pad();


Output

abcdefg
1234
***abcdefg
^#^#^#1234

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments