×

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces.

Note:- Java String trim() method doesn't trim or omit middle spaces.

Syntax:

public String trim()

Returns:

It returns string with omitted leading and trailing spaces.

Java String trim() method example 1

public class JavaStringTrimEx1
{
       public static void main(String[] args) {
              // String declaration with trailing and leading space
              String inputValue = "java String trim() method example by tutorialandexample.com";                                             
              // removing the white spaces
              String trimValue = inputValue.trim();
              System.out.println(trimValue);
       }
}

Output:

java String trim() method example by tutorialandexample.com

Java String trim() method example 2

public class JavaStringTrimEx2
{
       public static void main(String[] args) {
              // String declaration with trailing and leading space
              String inputValue = "tutorialandexample.com";                                            
              System.out.println("without trim:" +inputValue);
              // removing the white spaces
              String trimValue = inputValue.trim();
              System.out.println("After trim  :" +trimValue);
       }
}

Output:

without trim: tutorialandexample.com                                             
After trim  :tutorialandexample.com

Java String trim() method example 3

public class JavaStringTrimEx3
{
      public static void main(String[] args)
      {     
      // trims the trailing and leading spaces
    String s = "    This tutorial is written by Roy NK    ";
    System.out.println(s.trim());     
    // trims the leading spaces
    s = "  Tutorial And Example  ";
    System.out.println(s.trim());          
    }
}

Output:

This tutorial is written by Roy NK
Tutorial And Example

Java String trim() method example 4

public class JavaStringTrimEx4 { 
    public static void main(String[] args) { 
        String s1 ="   java string   "; 
        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:

17
java string  
11
java string

Related Topics

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

Compile-time Error in Java

In java, the execution of a program is stopped due to the occurrence of some problem known as an error. Errors are illegal operations that are carried out by the...

4 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.