String Utility Operations On Google Guava

Google Guava library supports various String Operations to speed up common operations in day to day programming. Below is a list that can help.

ASCII Operationscom.google.common.base.Ascii

  • ASCII constants including various types of white spaces like carriage return, new line, space etc, Signals like escape, delete etc, other communication signals and more.
  • equalsIgnoreCase – Much faster than implementation in java.lang.String. Works well for ASCII strings. Read the documentation and analyze your use case before using this.
  • Other common String operations like toUpperCase, toLowerCase, truncate etc.

Joiner
As the name depicts, the operation is to perform splitting a single string or joining multiple strings.
Joiner is used to join a String with other Strings or with an Iterable of Strings.

//each of these output a:b
Joiner.on(":").join("a", "b");
Joiner.on(":").appendTo(new StringBuilder(), new String[]{"a", "b"}).toString();
Joiner.on(":").join(new String[]{"a", "b"});

// below showcases handling nulls, again output remains as a:b
Joiner.on(":").skipNulls().join(new String[]{"a", null, "b"}); // skip null
Joiner.on(":").useForNull("b").join(new String[]{"a", null}); // replace null with constant

Below examples represent the way of handling null values.

// below showcases handling nulls, again output remains as a:b
Joiner.on(":").skipNulls().join(new String[]{"a", null, "b"}); // skip null
Joiner.on(":").useForNull("b").join(new String[]{"a", null}); // replace null with constant

It can even be used to format the Map representation as String.

Map m = new HashMap();
m.put("a", "1");
m.put("b", "2");
Joiner.on(":").withKeyValueSeparator("-").join(m); //output = "a-1:b-2"

Splitter

Splitter is used to split a String into multiple parts based on a separator. It has options to limit the splitting operation, handle trimming of parts with leading / trailing white spaces.

Splitter.on(",").split("a,b");
Splitter.on(",").omitEmptyStrings().split("a,,b");
Splitter.on(",").omitEmptyStrings().split("a,b,");
Splitter.on(",").omitEmptyStrings().splitToList("a,,b");
Splitter.on(",").limit(2).split("a,b,c");
Splitter.on(",").trimResults().split(" a , b ");

It also can be used to read a String representation of map and convert back to the key value pairs of Map.

// in below code, map m and result have same contents
Map<String, String> m = new HashMap<>();
m.put("a", "1");
m.put("b", "2");

String s = Joiner.on(",").withKeyValueSeparator(" has ").join(m);
Map<String, String> result = Splitter.on(",").withKeyValueSeparator(" has ").split(s);

Strings

com.google.common.base.Stringshas multiple static methods for string operations like padding at the beginning / end, empty and null interconversion, repeating the string for few times,  identifying the common prefix / suffix and to check if a given String is empty or null.

commonPrefix("aab", "aaaaac"); // returns: "aa"
commonSuffix("baabb", "abb"); // returns: "abb"
padEnd("ab", 5, 'c'); // returns: "abccc"
padStart("bc", 5, 'a'); // returns: "aaabc"
repeat("ab", 3); // returns: "ababab"
nullToEmpty(null); // returns: ""
emptyToNull(""); // returns null
isNullOrEmpty(null); // returns true
isNullOrEmpty(""); // returns true