×

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

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.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Streams in Java

The conventional Java SE 8 version came with many new peculiarities, out of which the most striking of which are assuredly lambda expressions and the method references. Streams and Streams...

14 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.