java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to remove leading and trailing white space from string?

package com.javacoderanch.example.lang;

public class TrimStringExample {
    public static void main(String[] args) {
	String text = "     tattarrattat     ";
	
	//
	// The trim() method will result a new string object with a leading and
	// trailing while space removed.
	//
	System.out.println("Result: " + text.trim());
    }
}