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>
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".

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 - Converting From Lower Hyphen 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_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));
   }
}

Output

Run the GuavaTester and verify the output −

testData

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

Example - Converting From Upper Underscore Case to Upper 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.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));
   }
}

Output

Run the GuavaTester and verify the output −

TestData
Advertisements