[Solved] String formatting under a certain character [closed]

Introduction

String formatting is a powerful tool for manipulating text in a variety of ways. It allows you to control the appearance of text by changing the way it is displayed, such as by adding a certain character to the beginning or end of a string. This can be useful for creating a consistent look and feel for text, or for making sure that text is displayed in a certain way. In this article, we will discuss how to use string formatting to add a certain character to the beginning or end of a string.

Solution

//Using the String.prototype.padEnd() method

let str = “Hello”;
let char = “*”;
let maxLength = 10;

let formattedString = str.padEnd(maxLength, char);

console.log(formattedString); // Output: “Hello*****”


Since in the comment, the OP attempted to use String.format(), here is an approach to consider. Rather than trying to get the number to align right with the “%23d”, align the word and the count separately.

String.format("%-23s%2d:", getWord(), count);

The %-23d will format the getWord() in 23 spaces, left aligned, then the %2d will right align the “count”.

Example output:

Hello...................9:
Goodbye................42:
whatever...............17:

Note I just used the same .replace() approach as the OP for quickness sake.

See this attempt here at ideone.com

solved String formatting under a certain character [closed]