×

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

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 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.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.