How would I remove the first white-space in Java?
Right now I am using this:
if (str.charAt(0) == ' ') str = str.replace(" ", "");
Just use str.trim()
to get rid of all leading and trailing spaces.
Use replaceFirst()
instead of replace()
.
TO get rid of all leading spaces you can use
str = str.replaceFirst("^ *", "");
The ^
is just to make sure that the spaces are actually at the start of the string, which it seems like you wanted. If that is not the case, just remove it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With