The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

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
1Joiner
Utility to join objects, string etc.
2Splitter
Utility to split string.
3CharMatcher
Utility for character operations.
4CaseFormat
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.
5StringBuilder appendTo(StringBuilder builder, Iterable<?> parts)
Appends the string representation of each of the parts, using the previously configured separator between each, to builder.
6StringBuilder appendTo(StringBuilder builder, Iterator<?> parts)
Appends the string representation of each of the parts, using the previously configured separator between each, to builder.
7StringBuilder appendTo(StringBuilder builder, Object[] parts)
Appends the string representation of each of the parts, using the previously configured separator between each, to builder.
8StringBuilder appendTo(StringBuilder builder, Object first, Object second, Object... rest)
Appends to builder the string representation of each of the remaining arguments.
9String join(Iterable<?> parts)
Returns a string containing the string representation of each of the parts, using the previously configured separator between each.
10String join(Iterator<?> parts)
Returns a string containing the string representation of each of the parts, using the previously configured separator between each.
11String join(Object[] parts)
Returns a string containing the string representation of each of the parts, using the previously configured separator between each.
12String join(Object first, Object second, Object... rest)
Returns a string containing the string representation of each argument, using the previously configured separator between each.
13static Joiner on(char separator)
Returns a joiner which automatically places separator between consecutive elements.
14static Joiner on(String separator)
Returns a joiner which automatically places separator between consecutive elements.
15Joiner skipNulls()
Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements.
16Joiner useForNull(String nullText)
Returns a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements.
17Joiner.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:
  • java.lang.Object
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
1static Splitter fixedLength(int length)
Returns a splitter that divides strings into pieces of the given length.
2Splitter limit(int limit)
Returns a splitter that behaves equivalently to this splitter but stops splitting after it reaches the limit.
3Splitter omitEmptyStrings()
Returns a splitter that behaves equivalently to this splitter, but automatically omits empty strings from the results.
4static Splitter on(char separator)
Returns a splitter that uses the given single-character separator.
5static Splitter on(CharMatcher separatorMatcher)
Returns a splitter that considers any single character matched by the given CharMatcher to be a separator.
6static Splitter on(Pattern separatorPattern)
Returns a splitter that considers any subsequence matching pattern to be a separator.
7static Splitter on(String separator)
Returns a splitter that uses the given fixed string as a separator.
8static Splitter onPattern(String separatorPattern)
Returns a splitter that considers any subsequence matching a given pattern (regular expression) to be a separator.
9Iterable<String> split(CharSequence sequence)
Splits sequence into string components and makes them available through an Iterator, which may be lazily evaluated.
10List<String> splitToList(CharSequence sequence)
Splits sequence into string components and returns them as an immutable list.
11Splitter 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).
12Splitter 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.
13Splitter.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.
14Splitter.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.
15Splitter.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:
  • java.lang.Object

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
1static CharMatcher ANY
Matches any character.
2static CharMatcher ASCII
Determines whether a character is ASCII, meaning that its code point is less than 128.
3static 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).
4static CharMatcher DIGIT
Determines whether a character is a digit according to Unicode.
5static 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.
6static CharMatcher JAVA_DIGIT
Determines whether a character is a digit according to Java's definition.
7static CharMatcher JAVA_ISO_CONTROL
Determines whether a character is an ISO control character as specified by Character.isISOControl(char).
8static CharMatcher JAVA_LETTER
Determines whether a character is a letter according to Java's definition.
9static CharMatcher JAVA_LETTER_OR_DIGIT
Determines whether a character is a letter or digit according to Java's definition.
10static CharMatcher JAVA_LOWER_CASE
Determines whether a character is lower case according to Java's definition.
11static CharMatcher JAVA_UPPER_CASE
Determines whether a character is upper case according to Java's definition.
12static CharMatcher NONE
Matches no characters.
13static CharMatcher SINGLE_WIDTH
Determines whether a character is single-width (not double-width).
14static CharMatcher WHITESPACE
Determines whether a character is whitespace according to the latest Unicode standard, as illustrated here.
Constructor(s)
S.N.Constructor & Description
1protected CharMatcher()
Constructor for use by subclasses.
Class Methods
SQLSQL Lite
1CharMatcher and(CharMatcher other)
Returns a matcher that matches any character matched by both this matcher and other.
2static CharMatcher anyOf(CharSequence sequence)
Returns a char matcher that matches any character present in the given character sequence.
3boolean apply(Character character)
Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead.
4String 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.
5int countIn(CharSequence sequence)
Returns the number of matching characters found in a character sequence.
6static 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.?
7int indexIn(CharSequence sequence)
Returns the index of the first matching character in a character sequence, or -1 if no matching character is present.
8int 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.
9static CharMatcher inRange(char startInclusive, char endInclusive)
Returns a char matcher that matches any character in a given range (both endpoints are inclusive).
10static CharMatcher is(char match)
Returns a char matcher that matches only one specified character.
11static CharMatcher isNot(char match)
Returns a char matcher that matches any character except the one specified.
12int lastIndexIn(CharSequence sequence)
Returns the index of the last matching character in a character sequence, or -1 if no matching character is present.
13abstract boolean matches(char c)
Determines a true or false value for the given character.
14boolean matchesAllOf(CharSequence sequence)
Returns true if a character sequence contains only matching characters.
15boolean matchesAnyOf(CharSequence sequence)
Returns true if a character sequence contains at least one matching character.
16boolean matchesNoneOf(CharSequence sequence)
Returns true if a character sequence contains no matching characters.
17CharMatcher negate()
Returns a matcher that matches any character not matched by this matcher.
18static CharMatcher noneOf(CharSequence sequence)
Returns a char matcher that matches any character not present in the given character sequence.
19CharMatcher or(CharMatcher other)
Returns a matcher that matches any character matched by either this matcher or other.
20CharMatcher precomputed()
Returns a char matcher functionally equivalent to this one, but which may be faster to query than the original; your mileage may vary.
21String removeFrom(CharSequence sequence)
Returns a string containing all non-matching characters of a character sequence, in order.
22String 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.
23String 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.
24String retainFrom(CharSequence sequence)
Returns a string containing all matching characters of a character sequence, in order.
25String toString()
Returns a string representation of this CharMatcher, such as CharMatcher.or(WHITESPACE, JAVA_DIGIT).
26String 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.
27String 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.
28String trimLeadingFrom(CharSequence sequence)
Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string.
29String 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:
  • java.lang.Object
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
1LOWER_CAMEL
Java variable naming convention, e.g., "lowerCamel".
2LOWER_HYPHEN
Hyphenated variable naming convention, e.g., "lower-hyphen".
3LOWER_UNDERSCORE
C++ variable naming convention, e.g., "lower_underscore".
4UPPER_CAMEL
Java and C++ class naming convention, e.g., "UpperCamel".
5UPPER_UNDERSCORE
Java and C++ constant naming convention, e.g., "UPPER_UNDERSCORE".
Methods
S.N.Method & Description
1Converter<String,String> converterTo(CaseFormat targetFormat)
Returns a Converter that converts strings from this format to targetFormat.
2String to(CaseFormat format, String str)
Converts the specified String str from this format to the specified format.
3static CaseFormat valueOf(String name)
Returns the enum constant of this type with the specified name.
4static 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
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com