How to make padding spaces results of a similar length when using non alphanumeric characters?

448 Views Asked by At

I am using javascript, and I want the output of the following console.log() to be of a same length.

const str1 = 'English';
console.log(str1.padEnd(10, '.'));

const str2 = '日本語';
console.log(str2.padEnd(10, '.'));

However, the current output is as following:

> "English..."
> "日本語......."

My desired output is as following:

> "English..."
> "日本語....."

I am trying my example based on the explanation here String.prototype.padEnd()

How to achieve this?

1

There are 1 best solutions below

2
Dhaivat Desai On

For doing this you can find the length of both strings using the length() methods of javascript and then add the padEnd method according to the difference

const str1 = 'English';
const str2 = '日本語';

const a=str1.length;
const b=str2.length;

const c= a-b;
console.log(str1);
console.log(str2.padEnd(c,'.'))

This will also make the whole thing dynamic as well