Updated July 28, 2023
Introduction to trim() Function in Java
The following article, trim() Function in Java, provides an outline for the functions in java. Basically, Java’s trim method gets rid of leading and trailing spaces. It is a built-in function. This method takes any string as input and check if it has any space character before and after the string, and then deletes the unnecessary white spaces wherever found and returns the edited string.
Java is one of the widely used and easy to learn programming language, a vast variety of methods and functions are available to ease the task. The trim method is one such method. As defined in Oracle Docs, the trim method is: returns a copy of the string, with leading and trailing whitespace excluded. Simply speaking, the trim method is used to delete whitespaces from the start and end of a string.
Let us now understand the standard syntax used for the implementation of the Trim method:
Syntax:
public String trim()
The above mentioned is the standard syntax for the trim method. Also known as Signature, it takes no parameters, and this trim method returns the edited string.
How does the trim() method work?
A trim method in java is a build-in function. Unicode Value for white space in Java is “\u0020”. When passed any string, the Trim method checks for the mentioned Unicode Value, which is for white space. If the Trim method finds matching Unicode Value, then it eliminates that and returns the newly trimmed string. It is also important to understand that this method does not delete or trim any middle spaces. Whenever the trim method is called, a new string object is returned.
Often, trim is unnecessary, and if the trim is removed in unwanted situations, it will improve the program performance. In the case of handling whitespaces between or inside the string, the trim method cannot be used. Regular expressions are a better choice for such problems.
Now that we have learned the definition, along with the standard syntax for the trim method, and with the understanding of how trim works, let’s see a trim method in action with some programs.
Examples of trim() Function in Java
We will now demonstrate the working of the Trim method with a simple example.
Example #1
Code:
class java_trim {
public static void main(String args[]) {
String line = " This is Java Trim Example with whitespaces ";
System.out.println(line); // without trim method
System.out.println(line.trim()); // with trim method
}
}
Output:
Code Interpretation: Here is our class java_trim; we have simply created the main class with a string variable name line, with String with whitespaces at the beginning and the end. Then we have printed the string as it is and then passed the trim method, and provided the output. The output is as expected, without any whitespaces. The above code will print two lines on proper compilation and execution, first with whitespaces and another without. Refer to the below attached screenshot for output.
As you can see, there are whitespaces at the beginning of the string, and for the second line, they are being omitted. Next, we’ll demonstrate another example with the trim() Function in java, and now we will track the difference between the length of the string.
Example #2
Code:
class java_trim {
public static void main(String[] args) {
String line1 =" This is just another example of Java Trim1 ";
System.out.println(line1.length());
System.out.println(line1); //this is without trim method
String line2 = line1.trim();
System.out.println(line2.length());
System.out.println(line2); //this is with trim method
}
}
Output:
Code Interpretation: In our second demonstration program, we have followed the same pattern as the previous but used a different method to keep track of the string changes. We started with our class java_trim, then the main class within. Declared first string with a string value that has to lead and trailing whitespaces. Then we simply printed the string’s length to keep track of changes, and then the original string printed. Then we have our second string, which is a trimmed version of the first string. Here you can see the implementation of the trim method for the first string. Finally, we have printed the length of the edited string and then the edited string.
Here, you will notice a difference in the length count of both strings. Refer to the below screenshot for output.
Now that we have understood various ways to implement the trim() function in java, let’s demonstrate a final example with String Array.
Example #3
Code:
public class java_trim1{
public static void main(String[] args) {
String[] values = { " java", "trim "};
for (String val : values) {
String trimmed = val.trim();
System.out.println("[" + val + "]");
System.out.println("[" + trimmed + "]\n");
}
}
}
Output:
Code Interpretation: In our final example, we have implemented the Trim method with a String Array. Code begins with Public class declaration java_trim1, then our main class. String[] declares an array of string, followed by the variable name and the values within. Then we have a for loop for the purpose of checking every value in the array. In, trim() Function in Java, for loop, pick every item in the array and apply the trim method. The trimmed value will be saved in another String variable named trimmed. Then we have our simple two print statements; the first statement will print the currently selected item from the array, as it is. Ex. “[ java]” will be printed. Then the edited version of the same item will be printed. “val” and ”trimmed” have the presently selected items from an array, val has the string in original, while trimmed returns the edited string. Refer below-attached screenshot for the proper output.
Specifically, Trim does not have any particular use, but the best application can be a file that is to be read and might have multiple unwanted white spaces; the trim method can be used, and the file can be cleaned. For different programming languages, differently named methods are available for a similar operation; for ruby or pearl, chop and chomp are used, and strip() is used with python.
Conclusion
We began with understanding what the Trim method is in Java. Then we understood the definitions available and the proper syntax. We understood the working of the Trim method, how it works and what are conditions. Later, we moved on to examples for proper demonstration, and we implemented examples with various methods in order to understand practical uses. Compiled code and output screenshots are attached for the respective code. Trim() is a clear and simple call.
Recommended Articles
This is a guide to trim() Function in Java. Here we discuss the introduction to trim() Function in Java; how does the trim() method work? Along with the respective examples. You can also go through our other related articles to learn more–