Guava - STRING UTILITIES
Guava introduces many advanced string utilities based on developers' experience in application development works. Following is the list of useful string based utilities:
| S.N. | Utility name & Description |
| 1 | Joiner Utility to join objects, string etc. |
| 2 | Splitter Utility to split string. |
| 3 | CharMatcher Utility for character operations. |
| 4 | CaseFormat Utility for changing string formats. |
Joiner Class
Joiner provides various methods to handle joining operations on string, objects, etc.
Class Declaration
Following is the declaration for
com.google.common.base.Joiner class:
@GwtCompatible
public class Joiner
extends Object
Class Methods
| S.N. | Method & Description |
| 1 | <A extends Appendable> A appendTo(A appendable, Iterable<?> parts) Appends the string representation of each of the parts, using the previously configured separator between each, to appendable. |
| 2 | <A extends Appendable> A appendTo(A appendable, Iterator<?> parts) Appends the string representation of each of the parts, using the previously configured separator between each, to appendable. |
| 3 | <A extends Appendable> A appendTo(A appendable, Object[] parts) Appends the string representation of each of the parts, using the previously configured separator between each, to appendable. |
| 4 | <A extends Appendable> A appendTo(A appendable, Object first, Object second, Object... rest) Appends to appendable the string Appends to appendable the string representation of each of the remaining arguments. |
| 5 | StringBuilder appendTo(StringBuilder builder, Iterable<?> parts) Appends the string representation of each of the parts, using the previously configured separator between each, to builder. |
| 6 | StringBuilder appendTo(StringBuilder builder, Iterator<?> parts) Appends the string representation of each of the parts, using the previously configured separator between each, to builder. |
| 7 | StringBuilder appendTo(StringBuilder builder, Object[] parts) Appends the string representation of each of the parts, using the previously configured separator between each, to builder. |
| 8 | StringBuilder appendTo(StringBuilder builder, Object first, Object second, Object... rest) Appends to builder the string representation of each of the remaining arguments. |
| 9 | String join(Iterable<?> parts) Returns a string containing the string representation of each of the parts, using the previously configured separator between each. |
| 10 | String join(Iterator<?> parts) Returns a string containing the string representation of each of the parts, using the previously configured separator between each. |
| 11 | String join(Object[] parts) Returns a string containing the string representation of each of the parts, using the previously configured separator between each. |
| 12 | String join(Object first, Object second, Object... rest) Returns a string containing the string representation of each argument, using the previously configured separator between each. |
| 13 | static Joiner on(char separator) Returns a joiner which automatically places separator between consecutive elements. |
| 14 | static Joiner on(String separator) Returns a joiner which automatically places separator between consecutive elements. |
| 15 | Joiner skipNulls() Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements. |
| 16 | Joiner useForNull(String nullText) Returns a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements. |
| 17 | Joiner.MapJoiner withKeyValueSeparator(String keyValueSeparator) Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise. |
Methods Inherited
This class inherits methods from the following class:
Example of Joiner Class
Create the following java program using any editor of your choice in say
C:/> Guava.
GuavaTester.java
import java.util.Arrays;
import com.google.common.base.Joiner;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testJoiner();
}
private void testJoiner(){
System.out.println(Joiner.on(",")
.skipNulls()
.join(Arrays.asList(1,2,3,4,5,null,6)));
}
}
Verify the Result
Compile the class using
javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
1,2,3,4,5,6
Splitter Class
Splitter provides various methods to handle splitting operations on string, objects, etc.
Class Declaration
Following is the declaration for
com.google.common.base.Splitter class:
@GwtCompatible(emulated=true)
public final class Splitter
extends Object
Class Methods
| S.N. | Method & Description |
| 1 | static Splitter fixedLength(int length) Returns a splitter that divides strings into pieces of the given length. |
| 2 | Splitter limit(int limit) Returns a splitter that behaves equivalently to this splitter but stops splitting after it reaches the limit. |
| 3 | Splitter omitEmptyStrings() Returns a splitter that behaves equivalently to this splitter, but automatically omits empty strings from the results. |
| 4 | static Splitter on(char separator) Returns a splitter that uses the given single-character separator. |
| 5 | static Splitter on(CharMatcher separatorMatcher) Returns a splitter that considers any single character matched by the given CharMatcher to be a separator. |
| 6 | static Splitter on(Pattern separatorPattern) Returns a splitter that considers any subsequence matching pattern to be a separator. |
| 7 | static Splitter on(String separator) Returns a splitter that uses the given fixed string as a separator. |
| 8 | static Splitter onPattern(String separatorPattern) Returns a splitter that considers any subsequence matching a given pattern (regular expression) to be a separator. |
| 9 | Iterable<String> split(CharSequence sequence) Splits sequence into string components and makes them available through an Iterator, which may be lazily evaluated. |
| 10 | List<String> splitToList(CharSequence sequence) Splits sequence into string components and returns them as an immutable list. |
| 11 | Splitter trimResults() Returns a splitter that behaves equivalently to this splitter, but automatically removes leading and trailing whitespace from each returned substring; equivalent to trimResults(CharMatcher.WHITESPACE). |
| 12 | Splitter trimResults(CharMatcher trimmer) Returns a splitter that behaves equivalently to this splitter, but removes all leading or trailing characters matching the given CharMatcher from each returned substring. |
| 13 | Splitter.MapSplitter withKeyValueSeparator(char separator) Returns a MapSplitter which splits entries based on this splitter, and splits entries into keys and values using the specified separator. |
| 14 | Splitter.MapSplitter withKeyValueSeparator(Splitter keyValueSplitter) Returns a MapSplitter which splits entries based on this splitter, and splits entries into keys and values using the specified key-value splitter. |
| 15 | Splitter.MapSplitter withKeyValueSeparator(String separator) Returns a MapSplitter which splits entries based on this splitter, and splits entries into keys and values using the specified separator. |
Methods Inherited
This class inherits methods from the following class:
Example of Splitter Class
Create the following java program using any editor of your choice in say
C:/> Guava.
GuavaTester.java
import com.google.common.base.Splitter;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testSplitter();
}
private void testSplitter(){
System.out.println(Splitter.on(',')
.trimResults()
.omitEmptyStrings()
.split("the ,quick, , brown , fox, jumps,
over, the, lazy, little dog."));
}
}
Verify the Result
Compile the class using
javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
[the, quick, brown, fox, jumps, over, the, lazy, little dog.]
Char Matcher Class
CharMatcher provides various methods to handle various JAVA types for char values.
Class Declaration
Following is the declaration for
com.google.common.base.CharMatcher class:
@GwtCompatible(emulated=true)
public final class CharMatcher
extends Object
Fields
| S.N. | Field & Description |
| 1 | static CharMatcher ANY Matches any character. |
| 2 | static CharMatcher ASCII Determines whether a character is ASCII, meaning that its code point is less than 128. |
| 3 | static CharMatcher BREAKING_WHITESPACE Determines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes). |
| 4 | static CharMatcher DIGIT Determines whether a character is a digit according to Unicode. |
| 5 | static CharMatcher INVISIBLE Determines whether a character is invisible; that is, if its Unicode category is any of SPACE_SEPARATOR, LINE_SEPARATOR, PARAGRAPH_SEPARATOR, CONTROL, FORMAT, SURROGATE, and PRIVATE_USE according to ICU4J. |
| 6 | static CharMatcher JAVA_DIGIT Determines whether a character is a digit according to Java's definition. |
| 7 | static CharMatcher JAVA_ISO_CONTROL Determines whether a character is an ISO control character as specified by Character.isISOControl(char). |
| 8 | static CharMatcher JAVA_LETTER Determines whether a character is a letter according to Java's definition. |
| 9 | static CharMatcher JAVA_LETTER_OR_DIGIT Determines whether a character is a letter or digit according to Java's definition. |
| 10 | static CharMatcher JAVA_LOWER_CASE Determines whether a character is lower case according to Java's definition. |
| 11 | static CharMatcher JAVA_UPPER_CASE Determines whether a character is upper case according to Java's definition. |
| 12 | static CharMatcher NONE Matches no characters. |
| 13 | static CharMatcher SINGLE_WIDTH Determines whether a character is single-width (not double-width). |
| 14 | static CharMatcher WHITESPACE Determines whether a character is whitespace according to the latest Unicode standard, as illustrated here. |
Constructor(s)
| S.N. | Constructor & Description |
| 1 | protected CharMatcher() Constructor for use by subclasses. |
Class Methods
| SQL | SQL Lite |
| 1 | CharMatcher and(CharMatcher other) Returns a matcher that matches any character matched by both this matcher and other. |
| 2 | static CharMatcher anyOf(CharSequence sequence) Returns a char matcher that matches any character present in the given character sequence. |
| 3 | boolean apply(Character character) Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead. |
| 4 | String collapseFrom(CharSequence sequence, char replacement) Returns a string copy of the input character sequence, with each group of consecutive characters that match this matcher replaced by a single replacement character. |
| 5 | int countIn(CharSequence sequence) Returns the number of matching characters found in a character sequence. |
| 6 | static CharMatcher forPredicate(Predicate<? super Character> predicate) Returns a matcher with identical behavior to the given Character-based predicate, but which operates on primitive char instances instead.? |
| 7 | int indexIn(CharSequence sequence) Returns the index of the first matching character in a character sequence, or -1 if no matching character is present. |
| 8 | int indexIn(CharSequence sequence, int start) Returns the index of the first matching character in a character sequence, starting from a given position, or -1 if no character matches after that position. |
| 9 | static CharMatcher inRange(char startInclusive, char endInclusive) Returns a char matcher that matches any character in a given range (both endpoints are inclusive). |
| 10 | static CharMatcher is(char match) Returns a char matcher that matches only one specified character. |
| 11 | static CharMatcher isNot(char match) Returns a char matcher that matches any character except the one specified. |
| 12 | int lastIndexIn(CharSequence sequence) Returns the index of the last matching character in a character sequence, or -1 if no matching character is present. |
| 13 | abstract boolean matches(char c) Determines a true or false value for the given character. |
| 14 | boolean matchesAllOf(CharSequence sequence) Returns true if a character sequence contains only matching characters. |
| 15 | boolean matchesAnyOf(CharSequence sequence) Returns true if a character sequence contains at least one matching character. |
| 16 | boolean matchesNoneOf(CharSequence sequence) Returns true if a character sequence contains no matching characters. |
| 17 | CharMatcher negate() Returns a matcher that matches any character not matched by this matcher. |
| 18 | static CharMatcher noneOf(CharSequence sequence) Returns a char matcher that matches any character not present in the given character sequence. |
| 19 | CharMatcher or(CharMatcher other) Returns a matcher that matches any character matched by either this matcher or other. |
| 20 | CharMatcher precomputed() Returns a char matcher functionally equivalent to this one, but which may be faster to query than the original; your mileage may vary. |
| 21 | String removeFrom(CharSequence sequence) Returns a string containing all non-matching characters of a character sequence, in order. |
| 22 | String replaceFrom(CharSequence sequence, char replacement) Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement character. |
| 23 | String replaceFrom(CharSequence sequence, CharSequence replacement) Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement sequence. |
| 24 | String retainFrom(CharSequence sequence) Returns a string containing all matching characters of a character sequence, in order. |
| 25 | String toString() Returns a string representation of this CharMatcher, such as CharMatcher.or(WHITESPACE, JAVA_DIGIT). |
| 26 | String trimAndCollapseFrom(CharSequence sequence, char replacement) Collapses groups of matching characters exactly as collapseFrom(java.lang.CharSequence, char) does, except that groups of matching characters at the start or end of the sequence are removed without replacement. |
| 27 | String trimFrom(CharSequence sequence) Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning and from the end of the string. |
| 28 | String trimLeadingFrom(CharSequence sequence) Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string. |
| 29 | String trimTrailingFrom(CharSequence sequence) Returns a substring of the input character sequence that omits all characters this matcher matches from the end of the string. |
Methods Inherited
This class inherits methods from the following classes:
Example of CharMatcher Class
Create the following java program using any editor of your choice in say
C:/> Guava.
GuavaTester.java
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testCharMatcher();
}
private void testCharMatcher(){
System.out.println(CharMatcher.DIGIT.retainFrom("mahesh123")); //
only the digits
System.out.println(CharMatcher.WHITESPACE.trimAndCollapseFrom("
Mahesh Parashar ", ' '));
// trim whitespace at ends, and replace/collapse whitespace into
single spaces
System.out.println(CharMatcher.JAVA_DIGIT.replaceFrom("mahesh123",
"*")); // star out all digits
System.out.println(CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom("mahesh123"));
// eliminate all characters that aren't digits or lowercase
}
}
Verify the Result
Compile the class using
javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
123
Mahesh Parashar
mahesh***
mahesh123
CaseFormat Class
CaseFormat is a utility class to provide conversion between various ASCII char formats.
Class Declaration
Following is the declaration for
com.google.common.base.CaseFormat class:
@GwtCompatible
public enum CaseFormat
extends Enum<CaseFormat>
Enum Constants
| S.N. | Enum Constant & Description |
| 1 | LOWER_CAMEL Java variable naming convention, e.g., "lowerCamel". |
| 2 | LOWER_HYPHEN Hyphenated variable naming convention, e.g., "lower-hyphen". |
| 3 | LOWER_UNDERSCORE C++ variable naming convention, e.g., "lower_underscore". |
| 4 | UPPER_CAMEL Java and C++ class naming convention, e.g., "UpperCamel". |
| 5 | UPPER_UNDERSCORE Java and C++ constant naming convention, e.g., "UPPER_UNDERSCORE". |
Methods
| S.N. | Method & Description |
| 1 | Converter<String,String> converterTo(CaseFormat targetFormat) Returns a Converter that converts strings from this format to targetFormat. |
| 2 | String to(CaseFormat format, String str) Converts the specified String str from this format to the specified format. |
| 3 | static CaseFormat valueOf(String name) Returns the enum constant of this type with the specified name. |
| 4 | static CaseFormat[] values() Returns an array containing the constants of this enum type, in the order they are declared. |
Methods Inherited
This class inherits methods from the following classes:
- java.lang.Enum
- java.lang.Object
Example of Case Format Class
Create the following java program using any editor of your choice in say
C:/> Guava.
GuavaTester.java
import com.google.common.base.CaseFormat;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testCaseFormat();
}
private void testCaseFormat(){
String data = "test_data";
System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat
.LOWER_CAMEL, "test-data"));
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat
.LOWER_CAMEL, "test_data"));
System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat
.UPPER_CAMEL, "test_data"));
}
}
Verify the Result
Compile the class using
javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
testData
testData
TestData