CodesJava

Easy learning with example program codes

trim() String functions in java


trim():Returns a copy of the string, with leading and trailing whitespace omitted.

Syntax: public String trim().

Example:

StringTrimExample.java

/**
 * This program is used to show the use of trim() method.
 * @author codesjava
 */
class TestString{
	String str = "   www.codesjava.com  ";
 
	/**
	 * This method is used to show the use of trim() method.
	 * @author codesjava
	 */
	public void trimString(){
		//will remove all leading and trailing whitespace. 
		System.out.println(str.trim());
	}
}
 
public class StringTrimExample {
	public static void main(String args[]){
		//creating TestString object
		TestString obj = new TestString();
 
		//method call
		obj.trimString();
	}
}

Output: 

www.codesjava.com

Download this example.
 
Next Topic: StringBuffer in java.
Previous Topic: length() String functions in java with example.

Related Topics:

String handling in java with example.
String Comparison in java with example.
String concatenation in java with example.
Substring in java with example.
How to write Immutable class in java with example.
toString() method in java with example.
Commonly used methods of String Class with example.
charAt(int index) String function in java with example.
startsWith(String prefix) and endsWith(String suffix) String functions in java.
indexOf(String str) and lastIndexOf(String str) String functions in java.
toLowerCase() and toUpperCase() String functions in java.
intern() String functions in java with example.
length() String functions in java with example.
trim() String functions in java with example.

 

Please follow and like us:
Posted in Java   

Core Java Tutorial



Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki