Java String trim() Method

Java String trim () Method

String in java

A string is a sequence of characters that is typically used to store and manipulate text. In many programming languages, including Java, a string is an object that represents a sequence of characters.

In Java, the String class is used to represent strings. The String class provides various methods for manipulating strings, such as concatenating strings, replacing characters, trimming whitespace, and more.

  • Strings are used frequently in Java programs to represent text, such as user input, program output, and data stored in text files.

To know more about String Class in java read the complete article.

String trim() Method:

The trim() method is a method of the String class in Java that removes leading and trailing white space from a string. It returns a new string with the white space removed, leaving all other characters unchanged.

  • The trim() method is often used to remove leading and trailing white space from user input, as it can make it easier to parse and process the input. It is also useful for comparing strings, as leading and trailing white space can sometimes cause two strings to appear different when they are actually the same.

The trim() method only removes leading and trailing white space. It does not remove white space from the middle of the string. If you need to remove white space from the middle of a string, you can use other methods such as replaceAll() or split().

Syntax:

public String trim()

Let’s look at a string-related Java program where the Java String trim() Method is used to perform an operation on the given string.

Example 1 : Java String trim() Method

Run
public class Main {
  public static void main(String[] args) {
    String myStr = "       PrepInsta!        ";
    System.out.println(myStr);
    System.out.println(myStr.trim());
  }
}

Output

       PrepInsta!        
PrepInsta!

Example 2 : Java string trim() Method

Run
public class Main {
  public static void main(String[] args) {
String str = "   Prepinsta\tPrepinsta Prime!\n";
str = str.trim().replaceAll("[\t\n\r]", "");
System.out.println(str);  // Outputs "Hello World!"
  }
}

Output

PrepinstaPrepinsta Prime!

Example 3: Java String toUpperCase Method

Run
public class Main {
  public static void main(String[] args) {
        String s1 ="  hey prepinsta and prepinsta prime  ";    
        System.out.println(s1.length());    
        System.out.println(s1); //Without trim()    
        String tr = s1.trim();    
        System.out.println(tr.length());    
        System.out.println(tr); //With trim()    
    }    
}

Output

37
  hey prepinsta and prepinsta prime  
33
hey prepinsta and prepinsta prime

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription