Introduction to Java String Trim
The String trim is a method in Java. trim() is used to remove the empty spaces from both ends of the string. The trim() method is available in java.lang.String package. In order to make use of the trim method, we must import this package. Java recognizes the empty characters based on its Unicode. Unicode of empty character in Java is ‘\u0020’. So while processing the trim() method java compiler checks this Unicode for empty spaces in the string then if it finds any empty spaces then it is removed from both the ends. Technically starting empty spaces are said to be leading empty spaces and ending empty spaces are said to be trailing spaces. By removing unnecessary empty spaces makes the readability better.
Real-Time Scenario: We know that every character in Java or any other programming language occupies some memory, the same way the empty character in Java also occupies space. In order to save this, we use the trim() method in Java.
How does the trim() method works in Java?
- The trim() method works only on string values. It removes the empty spaces from both the ends of the string values.
Signature:
public String trim()
Return Value: It returns the string with removing leading and trailing string as output.
Example:
“ | EDUCBA IS ONLINE COURSE PROVIDER | “ |
- In the above example at the front there 3 empty spaces and at the end 2 empty spaces.
- Remove these empty spaces we have to apply the trim method on the above string then we got the string as “EDUCBA IS ONLINE COURSE PROVIDER”.
Internal Implementation of method:
public String trim() {
int length = value.length;
int count = 0;
char[] characters = value;
while ((count < length) && (characters[count ] <= ' ')) {
count++;
}
while ((count < length) && (characters [length - 1] <= ' ')) {
length--;
}
return ((count > 0) || (length <value.length)) ? substring(count, length) : this;
}
Examples
Here are the following examples mention below
Example #1
Code:
//Java package
//package com.string.trim;
//creating class
public class JavaTrim {
// main method for run the Java application
public static void main(String args[]) {
// declaring a string
String string1 = " Hi, I am Paramesh ";
// displaying the output
System.out.println("String output after trimming :" + string1.trim());
// declaring a string
String string2 = " Hi, I am Amardeep ";
// displaying the output
System.out.println("String output after trimming :" + string2.trim());
}
}
Output:
Example #2
Code:
//Java package
//package com.string.trim;
import java.util.Scanner;
//creating class
public class JavaTrim {
// main method for run the Java application
public static void main(String args[]) {
Scanner scanner=new Scanner(System.in);
System.out.println("Please enter string with empty space starting and ending=>");
// Asking a user to enter input string
String string1 = scanner.nextLine();
// displaying the output
System.out.println("String output after trimming :" + string1.trim());
System.out.println("Please enter string with empty space starting and ending=>");
// Asking a user to enter input string
String string2 = scanner.nextLine();
// displaying the output
System.out.println("String output after trimming :" + string2.trim());
scanner.close();
}
}
Output:
Example #3
Code:
//Java package
//package com.string.trim;
import java.util.Scanner;
//creating class
public class JavaTrim {
// main method for run the Java application
public static void main(String args[]) {
Scanner scanner=new Scanner(System.in);
System.out.println("Please enter string with empty space starting and ending=>");
// Asking a user to enter input string
String string1 = scanner.nextLine();
System.out.println("String output before trimming: "+string1.trim());
// displaying the output
System.out.println("String output after trimming :" + string1.trim());
System.out.println("Please enter string with empty space starting and ending=>");
// Asking a user to enter input string
String string2 = scanner.nextLine();
System.out.println("String output before trimming: "+string2.trim());
// displaying the output
System.out.println("String output after trimming :" + string2.trim());
scanner.close();
}
}
Output:
Example #4
Code:
//Java package
//package com.string.trim;
import java.util.Scanner;
//creating class
public class JavaTrim {
// main method for run the Java application
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter string with empty space starting and ending=>");
// Asking a user to enter input string
String string1 = scanner.nextLine();
System.out.println(
"String output before trimming: " + string1.trim() + " and it's length is :" + string1.length());
// displaying the output
System.out.println(
"String output after trimming :" + string1.trim() + " and it's length is :" + string1.trim().length());
scanner.close();
}
}
Output:
Conclusion
String trim in Java is used to remove the empty spaces at the leading and trailing of the string. trim() method identifies and removes these empty spaces based on the Unicode of the empty space.
Recommended Articles
This is a guide to Java String Trim. Here we discuss How does this method works in Java and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –