×

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

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Array Slicing in Java

Array slicing is a method in Java for obtaining a subarray of a specified array. Assume a[] is an array. It contains eight items indexed from a[0] to a[7].a[] =...

3 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.