Introduction
Sometimes your strings have leading or trailing whitespace – spaces, tabs, newlines – that you don't want. The `trim()` method provides a simple way to remove this unwanted whitespace from the beginning and end of a string without affecting the characters in between.
Syntax
public String trim()
Parameters
Parameter | Description |
---|---|
None | This method does not take any parameters. |
Return Value
The trim()
method returns a new string with leading and trailing whitespace removed.
Examples
Example 1: Basic Trimming
This example shows how to trim a simple string containing spaces at the beginning and end.
public class TrimExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String trimmedStr = str.trim();
System.out.println("Original String: \"" + str + "\"");
System.out.println("Trimmed String: \"" + trimmedStr + "\"");
}
}
Original String: Hello, World!
Trimmed String: Hello, World!
In this example, the leading and trailing spaces are removed from str
before assigning it to trimmedStr
. The original string remains unchanged.
Example 2: Trimming with Tabs and Newlines
This demonstrates how `trim()` handles tabs and newlines, not just spaces.
public class TrimExample2 {
public static void main(String[] args) {
String str = "\t\n Hello World \n\t";
String trimmedStr = str.trim();
System.out.println("Original String: \"" + str + "\"");
System.out.println("Trimmed String: \"" + trimmedStr + "\"");
}
}
Original String:
Hello World
Trimmed String: Hello World
Notice how the tabs and newlines at both ends of the string are removed. The content between these characters remains unaffected.
Example 3: Trimming an Empty String
This example illustrates what happens when you try to trim an empty string or a string containing only whitespace.
public class TrimExample3 {
public static void main(String[] args) {
String str1 = "";
String str2 = " \t\n\t ";
String trimmedStr1 = str1.trim();
String trimmedStr2 = str2.trim();
System.out.println("Original String 1: \"" + str1 + "\"");
System.out.println("Trimmed String 1: \"" + trimmedStr1 + "\"");
System.out.println("Original String 2: \"" + str2 + "\"");
System.out.println("Trimmed String 2: \"" + trimmedStr2 + "\"");
}
}
Original String 1:
Trimmed String 1:
Original String 2:
Trimmed String 2:
When trimming an empty string, you get an empty string back. When trimming a string containing only whitespace, the result is also an empty string.
Example 4: Using Trim with User Input
This example shows how to use `trim()` when getting input from a user, which often contains leading or trailing spaces.
import java.util.Scanner;
public class TrimExample4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
String trimmedName = name.trim();
System.out.println("Hello, " + trimmedName + "!");
scanner.close();
}
}
This code prompts the user for their name and then trims any leading or trailing spaces before displaying a greeting. This ensures that the displayed name is clean and professional.