Python equivalent lstrip () string?

I would like to remove the leading spaces in the line, but not removing the trailing spaces - so trim () will not work. In python, I use lstrip (), but I'm not sure if there is an equivalent in Java.

As an example

"    foobar    "

should become

"foobar    "

I would also like to avoid using Regex, if at all possible.

Is there a built-in function in Java or do I need to create my own method for this? (and what is the shortest way I could achieve this)

+3
source share
4 answers

You can do this in regex:

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

StringUtils Apache Commons Lang, stripStart() ( ).

+6

Guava has CharMatcher.WHITESPACE.trimLeadingFrom(string). This in itself is no different from the versions of other software libraries of the same, but as soon as you are familiar with CharMatcher, there is a huge breadth of text processing operations that you will know how to perform in a sequential, performing manner.

+2
source

Source: https://habr.com/ru/post/1751352/


All Articles