In this article, we will discuss String’s trim() method which is used remove both leading and trailing white-spaces
1. String’s trim() method:
- This String method is used to remove trailing as well as leading white spaces from the invoking-string
1.1 Method Signature:
public String trim();
1.2 Returns:
- Return a String after removing both leading and trailing white spaces
2. Examples on trim() method:
In the below example,
- We will create 2 String objects and 2 String literals
- Print to console without invoking trim() method on any Strings
- Later, we will invoke trim() method on all 4 strings
- Finally, printing again the string to console
2.1 To trim both leading and trailing spaces
- to trim leading/trailing white-spaces from the invoking-string
- i.e.; to remove/eliminate white-spaces on both sides
StringTrimMethod.java
package in.bench.resources.string.methods;
public class StringTrimMethod {
public static void main(String[] args) {
// create 2 String objects
String strObject1 = new String(" this string object "
+ "is with leading white-space");
String strObject2 = new String(" this string object "
+ "has both leading and "
+ "trailing white-spaces ");
// create 2 String literals
String strLiteral3 = "this string literal is with "
+ "trailing white-space ";
String strLiteral4 = " this string literal "
+ "has both leading and "
+ "trailing white-spaces ";
// lets print all strings without trim() method
System.out.println("All Strings without invoking "
+ "trim() method\n");
System.out.println("String Object 1 contents : "
+ strObject1);
System.out.println("String Object 2 contents : "
+ strObject2);
System.out.println("String Literal 3 contents : "
+ strLiteral3);
System.out.println("String Literal 4 contents : "
+ strLiteral4);
// lets print all strings without trim() method
System.out.println("\n\nAll Strings after invoking "
+ "trim() method "
+ "to remove white-spaces\n");
System.out.println("String Object 1 contents : "
+ strObject1.trim());
System.out.println("String Object 2 contents : "
+ strObject2.trim());
System.out.println("String Literal 3 contents : "
+ strLiteral3.trim());
System.out.println("String Literal 4 contents : "
+ strLiteral4.trim());
}
}
Output:
All Strings without invoking trim() method
String Object 1 contents : this string
object is with leading white-space
String Object 2 contents : this string
object has both leading and trailing white-spaces
String Literal 3 contents : this string
literal is with trailing white-space
String Literal 4 contents : this string
literal has both leading and trailing white-spaces
All Strings after invoking trim() method to remove white-spaces
String Object 1 contents :
this string object is with leading white-space
String Object 2 contents :
this string object has both leading and trailing white-spaces
String Literal 3 contents :
this string literal is with trailing white-space
String Literal 4 contents :
this string literal has both leading and trailing white-spaces
Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!