Which of these method of class String is used to remove leading and trailing whitespace

Learn to use String class’s strip(), stripLeading() and stripTrailing() methods to remove unwanted white spaces from a given string in Java 11.

1. Using String strip() APIs – Java 11

Since Java 11, String class includes 3 more methods that help in removing extra white spaces. These methods use Character.isWhitespace(char) method to determine a white space character.

  • String strip() – returns a string whose value is given string, with all leading and trailing white space removed. Please note that String.trim() method also produces the same result.
  • String stripLeading() – returns a string whose value is given string, with all leading white space removed.
  • String stripTrailing() – returns a string whose value is given string, with all trailing white space removed.
String str = "  Hello World !!   ";

System.out.println( str.strip() );      //"Hello World !!"

System.out.println( str.stripLeading() ); //"Hello World !!   "

System.out.println( str.stripTrailing() );  //"  Hello World !!"

See Also: Removing Leading Spaces and Removing Trailing Spaces from a String

2. Using Regex

If you are not using Java 11 then you can use regular expressions to trim the white spaces (including tabs) around a string.

Regular expressionDescription
^[ \t]+|[ \t]+$ Remove leading and trailing white spaces
^[ \t]+ Remove only leading white spaces
[ \t]+$ Remove only trailing white spaces
String str = "  Hello World !!   ";

System.out.println( str.replaceAll("^[ \t]+|[ \t]+$", "") );  //"Hello World !!"

System.out.println( str.replaceAll("^[ \t]+", "") );      //"Hello World !!   "

System.out.println( str.replaceAll("[ \t]+$", "") );      //"  Hello World !!"

Drop me your questions related to how to remove white spaces and tabs from a string in Java.

Happy Learning !!

The Java String class trim() method eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in Java string checks this Unicode value before and after the string, if it exists then the method removes the spaces and returns the omitted string.

The string trim() method doesn't omit middle spaces.

Signature

The signature or syntax of the String class trim() method is given below:

Returns

string with omitted leading and trailing spaces

Internal implementation

Java String trim() Method Example

FileName: StringTrimExample.java

Test it Now

Output

hello string   javatpoint
hello stringjavatpoint   

Java String trim() Method Example 2

The example demonstrates the use of the trim() method. This method removes all the trailing spaces so the length of the string also reduces. Let's see an example.

FileName: StringTrimExample2.java

Output

22
  hello java string   
17
hello java string

Java String trim() Method Example 3

The trim() can be used to check whether the string only contains white spaces or not. The following example shows the same.

FileName: TrimExample3.java

Output

The string contains characters other than white spaces 

The string contains only white spaces

Java String trim() Method Example 4

Since strings in Java are immutable; therefore, when the trim() method manipulates the string by trimming the whitespaces, it returns a new string. If the manipulation is not done by the trim() method, then the reference of the same string is returned. Observe the following example.

FileName: TrimExample4.java

Output

The string contains characters other than white spaces 

The string contains only white spaces


Which of these method of class String is used to remove leading and trailing whitespace
Which of these method of class String is used to remove leading and trailing whitespace
DownloadApp

  • Academic Resource

    Aptitude Data Interpretation Verbal Reasoning Non Verbal Reasoning Verbal Ability Programming General Knowledge Puzzle

  • Engineering

    Computer Engineering Electronics and Communication Electrical Engineering Mechanical Engineering Civil Engineering Biotechnology Architecture & Planning

  • Online Test

    Aptitude Test Data Interpretation Test Verbal Reasoning Test Non Verbal Reasoning Test Verbal Ability Test

  • Exams
  • More

    Full Forms हिंदी


Home » JAVA Programming » Strings » Question


Strings

Easy Questions

JAVA Programming

Java Basic

Basic Syntax

Object & Classes

Basic Datatypes

Variable Types

Modifier Types

Basic Operators

Loop Control

Decision Making

Numbers

Characters

Strings

Arrays

Date & Time

Regular Expressions

Methods

Files and I/O

Exceptions

Inheritance

Polymorphism

Abstraction

Encapsulation

Interfaces

Packages

Data Structures

Collections

Generics

Serialization

Networking

Multithreading

Applet

  1. Which of these method of class String is used to remove leading and trailing whitespaces?

    1. Trim()
    2. doTrim()
    3. startsWith()
    4. trim()
    5. None of these

Correct Option: D

trim()

Which of these method of class String is used to remove leading and trailing whitespace


Your comments will be displayed only after manual approval.

Which method is used to removes leading and trailing whitespace?

Use the Trim() method to remove leading and trailing whitespace from a string. Let's say the following is our string with white spaces in the beginning and the end. String str = " a "; Now, use the trim() method.

Which of these method of class string is used to remove leading and trailing Whitespaces a Startswith () B trim () C trim () d do trim ()?

The trim() method removes whitespace from both sides of a string.

Which of these method of Class 10 is used to remove leading and trailing whitespace SSAS?

Explanation: trim() method is used to remove leading and trailing whitespaces in a string.

Which string method can be used to remove the leading and trailing spaces in a string?

strip() method has no argument, it removes any leading and/or trailing whitespace from a string.