Follow on Facebook

Like and Share on Facebook

Sunday, January 25, 2015

Java String trim


Java String trim

The java string trim() method eliminates leading and trailing spaces. The unicode value of space character is '\u0020'. The trim() method in java string checks this unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

The string trim() method doesn't omits middle spaces.


Signature

The signature or syntax of string trim method is given below:

public String trim() 

Returns

string with omitted leading and trailing spaces

Java String trim() method example

public class StringTrimExample{  
public static void main(String args[]){  
String s1="  hello string   ";  
System.out.println(s1+"javatpoint");//without trim()  
System.out.println(s1.trim()+"javatpoint");//with trim()  
}}  

Expected Output:
hello string javatpoint
hello stringjavatpoint

No comments:

Post a Comment