Home > Blockchain >  How to remove the last empty keys in a string
How to remove the last empty keys in a string

Time:04-05

I have a string that looks like this:

"a               "

As you can see there is only one character that is not empty " ". How do I remove the last charaters of the string so only the valid one(s) will remain:

"a"

This can also be considered in cases where the first characters are empty:

"         a"

I cannot simply remove each empty key as they can be part of a more complex sentence like this:

"I love Js            "

CodePudding user response:

In order to be able to remove easily the trailing spaces I suggest that you use the trim() method.

a '.trim() // return 'a'

  • Related