Sunday, February 23, 2014

What is trim ?

    if (!totalFemale.equals("")) {           
            committeesInfo.setTotalFemale(Integer.valueOf(totalFemale.trim()));
        }else{
            committeesInfo.setTotalFemale(null);           
        }



Return Value:
It returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.



Example:
import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("   Welcome to Tutorialspoint.com   ");

      System.out.print("Return Value :" );
      System.out.println(Str.trim() );
 //System.out.println(Str);   // For This out put is : Return Value :   Welcome to Tutorialspoint.com
   }
}
This produces the following result:

Return Value :Welcome to Tutorialspoint.com



Another Example :

This method removes the blank spaces from both ends of the given string (Front and End). 
Here is the code of program:



import java.lang.*;

public class StringTrim{
  public static void main(String[] args) {
  System.out.println("String trim example!");
  String str = " RoseIndia";
  System.out.println("Given String :" + str);
  System.out.println("After trim :" +str.trim());
  }
}

http://www.roseindia.net/java/beginners/StringTrim.shtml

Another Example :

Example of String trim() in java, JSP
public class StringTrimExample {

    public static void main(String[] args) {
        
        String string = " Java String ";
        
        String str = string.trim();
        
        System.out.println("Output without trim :"+string);
        
        System.out.println("Output with trim :"+str);
    }
}
Output
Output without trim : Java String 
Output with trim :Java String

No comments:

Post a Comment