Java String.trim() Method

Java String.trim() Method

By Yashwant Chavan, Views 1136, Date 16-Nov-2016

In this tutorial you will learn about Java String trim() method. This method is used to trim whitespace from the beginning and end of a string.

tags java

Refer below steps

  • String.trim() method removes leading and trailing whitespace from the string.

Java String.trim() method example


public class StringTrimExample {

    public static void main(String args[]) {
        String hello = "   hello   ";
        System.out.println("Before trim():- " + hello + " Length:- " + hello.length());
        System.out.println("After trim():- " + hello.trim() + " Length:- " + hello.trim().length());


Before trim():-    hello    Length:- 11
After trim():- hello Length:- 5

Suggested Posts



Hi there! I am founder of and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][]