

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 whitespace from the beginning and end of a string
Use the trim() method to remove whitespace from the beginning and end of a string.
Let’s say the following is our string with whitespace.
str1 = " The Big Bang Theory ";
Now, let us trim all the whitespaces from the beginning and the end.
String str2 = str1.trim();
The following is the final example with output.
Example
public class Demo { public static void main(String[] args) { String str1 = " The Big Bang Theory "; System.out.println("String: "+str1); String str2 = str1.trim(); System.out.println("Updated string (trimming spaces): "+str2); } }
Output
String: The Big Bang Theory Updated string (trimming spaces): The Big Bang Theory
- Related Questions & Answers
- Remove extra whitespace from the beginning PHP?
- Java Program to remove leading and trailing whitespace from a string
- How to remove all whitespace from String in Java?
- Java Program to get the beginning and end date of the week
- Remove whitespace in Java string.
- C# Program to remove the end part of a string
- Java Program to check the beginning of a string
- Java Program to check the end of a string
- Java Program to remove the leading and trailing quotes from a string
- Add elements at beginning and end of LinkedList in Java
- Java Program to Remove All Whitespaces from a String
- Get beginning and end date from a specific year in MySQL
- How do I remove a substring from the end of a string in Python?
- How to remove leading and trailing whitespace from a MySQL field value?
- How to remove all trailing whitespace of string in Python?
Advertisements