Home »
Java Programs »
Java String Programs
How to trim a given string using String.trim() method in Java?
String.trim() method in Java: Here, we will learn how to trim a given string using String.trim() method in Java?
Given a string with leading and trailing spaces and we have to trim the spaces.
String.trim()
It is a method of String class; it returns string after removing trailing and leading spaces.
Consider the program:
Here, is a string with trailing and leading spaces and program will trim the string (remove the spaces).
import java.lang.*;
public class StringTrimPrg
{
public static void main(String []args)
{
String str=" www.includehelp.com ";
System.out.println("String omitting leading and trailing spaces: "+ str.trim());
}
}
Output
String omitting leading and trailing spaces: www.includehelp.com
Java String Programs »