How to Trim Space from String in Java


The example will show us how to trim or remove some leading and trailing space from string by trim method in Java.

Source Code

package com.beginner.examples;

public class StringTrimExample {

	public static void main(String[] args) {
		
		String  string = "     HelloWorld.   ";
		
		/*
		 * To remove leading and trailing space from string use trim method of Java String.
		 */
		String stringResult = string.trim();
		
		System.out.println("Before triming , String is : " + string);
		System.out.println("After triming , String is : " + stringResult);
		
	}

}

Output:

Before triming , String is :      HelloWorld.   
After triming , String is : HelloWorld.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments