All for Joomla All for Webmasters

Java String trim

The java string trim() method eliminates leading and trailing spaces. The unicode value of space character is ‘\u0020’. The trim() method in java string checks this unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

The string trim() method doesn’t omits middle spaces.


Signature

The signature or syntax of string trim method is given below:

  1. public String trim()

Returns

string with omitted leading and trailing spaces


Java String trim() method example

  1. public class StringTrimExample{
  2. public static void main(String args[]){
  3. String s1=”  hello string   “;
  4. System.out.println(s1+“makeforsure”);//without trim()
  5. System.out.println(s1.trim()+“makeforsure”);//with trim()
  6. }}

Output:

  hello string   makeforsure
hello stringmakeforsure
PinIt
submit to reddit

Leave a Reply

Top