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.
1 Response
[…] int length() […]