Java String.trim() Method

 Posted On  | Yashwant Chavan 

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.

Refer below steps

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

Java String.trim() method example

package com.technicalkeeda.app;

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());
    }
}

Output

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


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0322 seconds.