Search code examples
javastringtrim

Java equivalent of python's String lstrip()?


I'd like to remove the leading whitespace in a string, but without removing the trailing whitespace - so trim() won't work. In python I use lstrip(), but I'm not sure if there's an equivalent in Java.

As an example

"    foobar    "

should become

"foobar    "

I'd also like to avoid using Regex if at all possible.

Is there a built in function in Java, or do I have to go about creating my own method to do this? (and what's the shortest way I could achieve that)


Solution

  • You could do this in a regular expression:

    "    foobar    ".replaceAll("^\\s+", "");