Skip to content
Advertisement

Tag: trim

How to simplify String concat in java?

Given the following code: I am trying and discussing in the team how the simpliest code could look like while one could use also constructs like: org.apache.commons.lang3.StringUtils: defaultString() MoreObjects.firstNonNull(user.getVorname(), Strings.EMPTY) A possible test could be like (expected results are visible here too): Any ideas welcome… what could I try? BTW: java 17 is allowed 🙂 The following code seemss to

Remove every space EXCEPT leading spaces

I need to remove every spaces from a String EXCEPT leading spaces. I have some strings that look like this : And I am trying to achieve this : That’s like a reverse trim(). What’s the most efficient way to go about it ? Answer You can use replaceAll with this regex (?<=S)(s+)(?=S) like this : Examples of input &

Trim String in Java while preserve full word

I need to trim a String in java so that: The quick brown fox jumps over the laz dog. becomes The quick brown… In the example above, I’m trimming to 12 characters. If I just use substring I would get: The quick br… I already have a method for doing this using substring, but I wanted to know what is

Adding whitespace in Java

There is a class trim() to remove white spaces, how about adding/padding? Note: ” ” is not the solution. Answer I think you are talking about padding strings with spaces. One way to do this is with string format codes. For example, if you want to pad a string to a certain length with spaces, use something like this: In

Advertisement