StringUtils trim

By | April 5, 2020

It removes spaces from both ends of the string.

Examples:

Basically, it removes control characters (char <= 32) from both ends of this String, handling null by returning null.

The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

Other trim methods:

  1. trimToEmpty
  2. trimToNull

1. trimToEmpty:

This method removes control characters (char <= 32) from both ends of the string and returns an empty String (“”) if the Stringis empty (“”) after the trim or if it is null.

Examples:

2 trimToNull:

This method removes control characters (char <= 32) from both ends of this String and returns null if the String isempty (“”) after the trim or if it is null.

Examples:

 

 

If you want to remove a space or other than a space character(s) in front and end of the string, you can use strip method. By using this method, you can define a specific character and method uses this character to trim the string.

strip:

To strip whitespace use strip(String).

To trim your choice of characters, use the strip(String, String) method.

Examples:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *