Table of Contents
What are Whitespace Characters?
Whitespace is defined as non-printing characters that take up spaces in a document, and it can include tabs, line breaks, and spaces.
It’s important to keep your strings clean of whitespaces because they can break formatting.
For example, you might have a word in your string that needs to be bolded but if the word has whitespace after it then the word will not be bolded.
Remove Whitespace Characters with trim() Method
The method returns a new string stripped of all extraneous white space and only contains what’s left over – so if it were ” Hello world “, then the output would just be “Hello world” without any blank spaces.
In a multi-line string, the trim() method can remove whitespaces in both ends too.