HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java remove trailing whitespaces from String

Java remove trailing whitespaces from String

Java program to remove trailing whitespaces from a String. This example will help you to trim only trailing whitespaces from a String in Java.

1. Java program to remove only trailing spaces with regular expression

This example utilizes regular expression to find all trailing white spaces. Once you have all trailing while spaces, you can replace them with empty string.

String regex = "\\s+$";
		
String originalString1 = "howtodoinjava.com   ";	//3 trailing spaces
String originalString2 = "   howtodoinjava.com   ";	//3 leading and trailing spaces

String trimmedString1 = originalString1.replaceAll(regex, "");
String trimmedString2 = originalString2.replaceAll(regex, "");

System.out.println(trimmedString1);
System.out.println(trimmedString2);

Program output.

howtodoinjava.com
   howtodoinjava.com

2. Java remove trailing whitespaces with Character.isWhitespace() method

This method iterate the characters of string from end. It checks if character is white space character then it replaces it with empty character.

This iteration goes on until a non-white space character is found.

public class StringExample 
{
	public static void main(String[] args) 
	{
		String originalString1 = "howtodoinjava.com   "; // 3 trailing spaces
		String originalString2 = "   howtodoinjava.com   "; // 3 leading and trailing spaces

		System.out.println(removeTrailingSpaces(originalString1));
		System.out.println(removeTrailingSpaces(originalString2));
	}

	public static String removeTrailingSpaces(String param) 
	{
		if (param == null)
			return null;
		int len = param.length();
		for (; len > 0; len--) {
			if (!Character.isWhitespace(param.charAt(len - 1)))
				break;
		}
		return param.substring(0, len);
	}
}

Program output.

howtodoinjava.com
   howtodoinjava.com

3. Java program to remove leading and trailing spaces

If you want to remove all leading and trailing spaces from string, then best way is to use String.trim() method.

String originalString1 = "howtodoinjava.com   "; // 3 trailing spaces
String originalString2 = "   howtodoinjava.com   "; // 3 leading and trailing spaces

String trimmedString1 = originalString1.trim();
String trimmedString2 = originalString2.trim();

System.out.println(trimmedString1);
System.out.println(trimmedString2);

Program output.

howtodoinjava.com
howtodoinjava.com

Use above given examples to trim trailing spaces from String in Java.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces