Trimming Whitespace from the Start and End of a String
Removing Whitespace from the Start: trimStart()
The trimStart()
method of String instances removes whitespace from the beginning (leading whitespace) of a string. This method returns a new string without modifying the original string.
- The method does not modify the original string but returns a new string with trailing whitespace removed.
Example :
Using trimStart()
to remove leading whitespace:
In this example:
- The original string has leading whitespace before the text
βHello, World!β
. - The
trimStart()
function removes this leading whitespace, leaving the rest of the string intact.
Removing Whitespace from the End: trimEnd()
The trimEnd()
method of String instances removes whitespace from the end (trailing whitespace) of a string. This method returns a new string without modifying the original string.
- The method does not modify the original string but returns a new string with trailing whitespace removed.
Example:
Using trimEnd()
to remove trailing whitespace:
In this example:
- The original string has trailing whitespace after the text
βHello, World!β
. - The
trimEnd()
function removes this trailing whitespace, leaving the rest of the string intact.
Notes
- The
trimStart()
method removes whitespace only from the beginning of the string. - The
trimEnd()
method removes whitespace only from the end of the string. - Neither method changes the original string; they return a new string instead.
trimLeft()
andtrimRight()
are aliases fortrimStart()
andtrimEnd()
respectively.