CaseFormat Class in Java


The CaseFormat class is a utility class for converting between various ASCII case formats −

Modifier and TypeMethod and Description
Objectclone()
Overrides Cloneable.
booleanequals(Object obj)
Overrides equals.
String.format(double number)
Specialization of format.
abstract StringBufferformat(double number, StringBuffer toAppendTo, FieldPosition pos)
Specialization of format.
Stringformat(long number)
Specialization of format.
abstract StringBufferformat(long number, StringBuffer toAppendTo, FieldPosition pos)
Specialization of format.

Example

Let us now see an example to implement the CaseFormat class with java file 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"));
   }
}

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

Output

This will produce the following output −

testData
testData
TestData

Updated on: 02-Jan-2020

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements