Q:

How to trim a given string using String.trim() method in Java?

belongs to collection: Java String Programs

0

Given a string with leading and trailing spaces and we have to trim the spaces.

String.trim()

It is a method of String class; it returns string after removing trailing and leading spaces.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

Here, is a string with trailing and leading spaces and program will trim the string (remove the spaces).

import java.lang.*;

public class StringTrimPrg
{
	public static void main(String []args)
	{
		String str="   www.includehelp.com      ";
		System.out.println("String omitting leading and trailing spaces: "+ str.trim());
	}
}

Output

String omitting leading and trailing spaces: www.includehelp.com

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to spilt string in substrings using String.spl... >>
<< How to calculate length of the string using String...