Guava - 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

Sr.No 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

Sr.No 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 CaseFormat 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
guava_string_utilities.htm
Advertisements