String methods – trim() and length()

I this post, we will discuss about the string methods – trim() and length().

Let’s understand them one by one with examples.

trim():

The method trim() is used to remove leading and trailing white spaces from the input String.

e.g. Suppose, If we have a string ”   Name  “.  After using trim , String will remove the whitespaces i.e.  “Name”.

public class ExampTrim {
	public static void main(String[] args) {

		String val1 = "     How are you?  ";
		System.out.println("After trimming the string - "+val1.trim() +"??");
	}
}
Output: After trimming the string – How are you???

length():

It is used to find out the no. of character in the string ,that is nothing but the length of the string.

public class ExampTrim {
	public static void main(String[] args) {

		String val1 = "Hello World";
		System.out.println("The length of the string is - "+val1.length());
	}
}
Output: The length of the string is – 11
Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

1 Response

  1. August 6, 2015

    […] int length() […]