- 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
The correct way to trim a string in Java.
The trim() method of the java.lang.String class returns a copy of the string omitting the leading and trailing whitespace.
Example
import java.lang.*; public class StringDemo { public static void main(String[] args) { // string with leading and trailing white space String str = " This is TutorialsPoint "; System.out.print("Before trim = "); System.out.println(".." + str + ".."); // leading and trailing white space removed System.out.print("After trim = "); System.out.println(".." + str.trim() + ".."); } }
Output
Before trim = .. This is TutorialsPoint .. After trim = ..This is TutorialsPoint..
Advertisements