Strip extra whitespace

If you have a String which might contains some extra "trailing" whitespace or extra "leading" whitespace, you can remove that by using the .strip method.

This will give a new String with both the leading and trailing whitespace removed.

void main() {
    String message = "   Happy Valentines Day.   ";

    System.out.print(message.strip());
    System.out.println("|");
}

If you want to just remove the leading whitespace, you can use .stripLeading.

void main() {
    String message = "   Happy Valentines Day.   ";

    System.out.print(message.stripLeading());
    System.out.println("|");
}

And to remove only trailing whitespace, .stripTrailing.

void main() {
    String message = "   Happy Valentines Day.   ";

    System.out.print(message.stripTrailing());
    System.out.println("|");
}

All of these methods are useful when you get input from human beings. Humans are generally pretty bad at seeing if they hit the spacebar one too many times.