Write a program to Trim a given string using String.trim() method


The code demonstrates the usage of the trim() method in Java's String class. The trim() method removes any leading and trailing whitespace characters in a String object. In this case, the str variable contains extra spaces at the beginning and end of the string, which are removed using the trim() method.

Source Code

import java.lang.*;
public class String_Method
{
	public static void main(String []args)
	{
		String str = "   String Function      ";
		System.out.println("String omitting Leading and Trailing Spaces : "+ str.trim());
	}
}

Output

String omitting Leading and Trailing Spaces : String Function

Example Programs