Learn to use Since Java 11, See Also: Removing Leading Spaces and
Removing Trailing Spaces from a String If you are not using Java 11 then you can use regular expressions to trim the white spaces [including tabs] around a string. Drop me your questions related to how to remove white spaces and tabs from a string in Java. Happy Learning !!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
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.trim[]
method also produces the same
result.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 !!"
2. Using Regex
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 !!"
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 NowOutput
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
-
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
Which of these method of class String is used to remove leading and trailing whitespaces?
- Trim[]
- doTrim[]
- startsWith[]
- trim[]
- None of these
Correct Option: D
trim[]
Your comments will be displayed only after manual approval.