Remove leading whitespace from a string

2021-06-05

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

The Strign class has a method called String#trim that removes whitespace from both ends of a string. If the whitespace at both ends is contiguous, it will also be removed.

So is there a method to remove whitespace from the left or right end only? I searched for it, but could not find it. So let’s try to create a method to remove only the leftmost whitespace.

String#trim method

Let’s introduce the String#trim method first. This method will return a new String object with consecutive whitespace removed from both ends. The original String will not be changed. The original String is not changed (the String object is immutable).

The following program defines a String variable s, and stores the result of the trim into a String variable t. The length is also output to check if the spaces on the right side have been removed.

Source
public class HelloTrim {

	public static void main(String[] args) {
        String s = "   Hello       ";
        String t = s.trim();

        System.out.println("\"" + s + "\"");
        System.out.println("length of s: " + s.length());

        System.out.println("\"" + t + "\"");
        System.out.println("length of t: " + t.length());
    }

}
Execution results
"   Hello       "
length of s: 15
"Hello"
length of t: 5

In the above execution result, the length of the string is output. You can see that all whitespace on both sides has been removed.

Remove white space on the left side only

We will now consider how to remove whitespace from the left side only. The procedure is as follows

  1. Define the int-type variable startPos.
  2. Repeat the following until the repetition counter i is between 0 and less than s.length().
    1. Case where s.charAt(i) is not blank
      1. Store the value of variable i in variable startPos.
      2. Exit the repetition.
  3. Return s.substring(startPos).

The following are the source and execution results.

Source
public class HelloTrimLeft {

    public static void main(String[] args) {
        String s = "   Hello       ";
        System.out.println("\"" + s + "\"");
        System.out.println("length of s: " + s.length());

        s = trimLeft(s);
        System.out.println("\"" + s + "\"");
        System.out.println("length of s: " + s.length());
    }

    private static String trimLeft(String s) {
        int startPos = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                startPos = i;
                break;
            }
        }

        return s.substring(startPos);
    }

}
Execution results
"   Hello       "
length of s: 15
"Hello       "
length of s: 12

Looking at the length, you can see that only the left side whitespace has been removed.

But how often do you have to remove spaces from the left side? I don’t think it happens very often. The program I made this time may be a little useful as an exercise for the charAt and substring methods.

That’s all. I hope this is helpful to you.

Articles next and previous in the same category