Java remove leading whitespaces from String

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

1. Java program to remove leading whitespaces with regular expression

1.1. String.replaceAll() Method

This example utilizes regular expression in replaceAll() method to find all leading white spaces. Once you have all leading while spaces, you can replace them with empty string.

String regex = "^\\s+";
		
String originalString1 = "   howtodoinjava.com";	//3 leading 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       //there are 3 whitespaces at the end

1.2. String.replaceFirst() Method

Alternatively, you can use replaceFirst() also. It takes a regular expression and search the first occurrence in beginning of the string. Then method replaces the matched string part with string value passed as second argument.

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

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

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

Program output.

howtodoinjava.com
howtodoinjava.com       //there are 3 whitespaces at the end

2. Java program to remove leading and trailing spaces

If you want to remove surrounding whitespaces from string, then best way is to use String.trim() method.

String originalString1 = "   howtodoinjava.com"; // 3 leading 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

3. Character.isWhitespace() [Not recommended]

This method iterate the characters of string from start. 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.

DO NOT try to reinvent to wheel and write your own implementation, until you have a very unique requirement which API is not able to solve.
public class StringExample 
{
	public static void main(String[] args) 
	{
		String originalString1 = "   howtodoinjava.com"; // 3 leading spaces
		String originalString2 = "   howtodoinjava.com   "; // 3 leading and leading spaces

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

	public static String removeLeadingSpaces(String param) 
	{
		if (param == null) {
			return null;
		}
		
		if(param.isEmpty()) {
			return "";
		}
		
		int arrayIndex = 0;
		while(true) 
		{
			if (!Character.isWhitespace(param.charAt(arrayIndex++))) {
				break;
			}
		}
		return param.substring(arrayIndex-1);
	}
}

Program output.

howtodoinjava.com
howtodoinjava.com       //there are 3 whitespaces at the end

Use above given examples to trim leading spaces from String in Java. If interested, you may want to read about removing only trailing whitespaces from a String in Java.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “Java remove leading whitespaces from String”

  1. Hi Lokesh,
    Thats very helpful. Thanks a lot.

    Just one observation:
    1.2. String.replaceFirst() heading says replaceFirst() method; but the example underneath is that of replaceAll.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.