Guava Strings utilities

31 10 2015

Almost every day you work with Strings. Guava has few convenient methods to make every day operations easier.

Strings class

Few static methods to notice:

  • Strings.padEnd(“foo”, 6, ‘xxx’) will pad provided string up to 6 chars with ‘x’ char. The result of this call will be “fooxxx”
  • Strings.padStart works analogously – it pads string from start. Strings.padStart(“foo”, 6, ‘xxx’) result will be “xxxfoo”
  • Strings.nullToEmpty(String) will turn null param to empty String of 0 length. If param was not null it will remain unchanged. It is advised to use to check method input params to avoid NPEs
  • Strings.emptyToNull(String) turns 0 length String to null object. If param war not an empty String, it will remain unchanged
  • Strings.isNullOrEmpty(String) replaces well known if(string != null && string.length() > 0) { … }. What a relief!

Take a look at a simple demo source code at my GitHub:



Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: