Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - String Interface
Guava introduces many advanced string utilities based on developers' experience in application development works. Following is the list of useful string based utilities −
Useful String Utilities
| Sr.No | 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. |
Example - Join Values while Skipping Null Values
GuavaTester.java
package com.tutorialspoint;
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)));
}
}
Output
Run the GuavaTester and verify the output.
1,2,3,4,5,6
Example - Omit Empty String while Spliting Strings
GuavaTester.java
package com.tutorialspoint;
import com.google.common.base.Splitter;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testSplitter();
}
private void testSplitter() {
Iterable<String> list = Splitter.on(',')
.trimResults()
.omitEmptyStrings()
.split("the ,quick, ,brown, fox, ,jumps, over, the, lazy, little, dog, ,,,,,.");
System.out.println(list);
}
}
Verify the Result
Run the GuavaTester and verify the output −
[the, quick, brown, fox, jumps, over, the, lazy, little, dog, .]
Example - Converting From Lower Underscore Case to Lower Camel Case
GuavaTester.java
package com.tutorialspoint;
import com.google.common.base.CaseFormat;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testCaseFormat();
}
private void testCaseFormat() {
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));
}
}
Output
Run the GuavaTester and verify the output −
testData
Advertisements