- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to remove leading and trailing whitespace from a string
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.
str.trim()
The following is the final example with output.
Example
public class Demo { public static void main(String[] args) { String str = " a "; System.out.println("Text = "+str); System.out.println("Text after using trim() method = " + str.trim()); } }
Output
Text = a Text after using trim() method = a
Advertisements