The code snippet below shows the use of trim method. The method trim removes extra spaces before and after the sting. It has nothing to do with the spaces in between the String words.
String str = " Java practice session. ";
System.out.println(str.trim());
Output: