Java Convert between Case Format of String using Google Guava

Tags: Google Guava CaseFormat String LOWER_HYPHEN LOWER_UNDERSCORE LOWER_CAMEL UPPER_CAMEL UPPER_UNDERSCORE

In this Java tutorial we learn how to use the com.google.common.base.CaseFormat class of Google Guava library to convert between various case formats of strings.

How to add Google Guava library to the Java project

To use the Google Guava library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'com.google.guava', name: 'guava', version: '30.1.1-jre'

To use the Google Guava library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1.1-jre</version>
</dependency>

To have more information about the Google Guava library you can visit the project home page at guava.dev

How to Convert between case format using CaseFormat.to() method

In the following Java code examples we show you how to use the CaseFormat.to() method to convert between different case format strings.

CaseFormatToExample1.java

import com.google.common.base.CaseFormat;

public class CaseFormatToExample1 {
    public static void main(String... args) {
        String inputString = "simple-solution";

        String result = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, inputString);

        System.out.println("Input String:\n" + inputString);
        System.out.println("\nOutput String:\n" + result);
    }
}
The output is:
Input String:
simple-solution

Output String:
simpleSolution

CaseFormatToExample2.java

import com.google.common.base.CaseFormat;

public class CaseFormatToExample2 {
    public static void main(String... args) {
        String inputString = "simple_solution";

        String result = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, inputString);

        System.out.println("Input String:\n" + inputString);
        System.out.println("\nOutput String:\n" + result);
    }
}
The output is:
Input String:
simple_solution

Output String:
SimpleSolution

CaseFormatToExample3.java

import com.google.common.base.CaseFormat;

public class CaseFormatToExample3 {
    public static void main(String... args) {
        String inputString = "SIMPLE_SOLUTION";

        String result = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, inputString);

        System.out.println("Input String:\n" + inputString);
        System.out.println("\nOutput String:\n" + result);
    }
}
The output is:
Input String:
SIMPLE_SOLUTION

Output String:
simpleSolution

How to Convert between case format using CaseFormat.converterTo() method

In the following Java code examples we show you how to use the CaseFormat.converterTo() method to convert between different case format strings.

CaseFormatConverterToExample1.java

import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;

public class CaseFormatConverterToExample1 {
    public static void main(String... args) {
        String inputString = "simple-solution";

        Converter<String, String> converter = CaseFormat.LOWER_HYPHEN.converterTo(CaseFormat.LOWER_CAMEL);
        String result = converter.convert(inputString);

        System.out.println("Input String:\n" + inputString);
        System.out.println("\nOutput String:\n" + result);
    }
}
The output is:
Input String:
simple-solution

Output String:
simpleSolution

CaseFormatConverterToExample2.java

import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;

public class CaseFormatConverterToExample2 {
    public static void main(String... args) {
        String inputString = "simple_solution";

        Converter<String, String> converter = CaseFormat.LOWER_UNDERSCORE.converterTo(CaseFormat.UPPER_CAMEL);
        String result = converter.convert(inputString);

        System.out.println("Input String:\n" + inputString);
        System.out.println("\nOutput String:\n" + result);
    }
}
The output is:
Input String:
simple_solution

Output String:
SimpleSolution

CaseFormatConverterToExample3.java

import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;

public class CaseFormatConverterToExample3 {
    public static void main(String... args) {
        String inputString = "SIMPLE_SOLUTION";

        Converter<String, String> converter = CaseFormat.UPPER_UNDERSCORE.converterTo(CaseFormat.LOWER_CAMEL);
        String result = converter.convert(inputString);

        System.out.println("Input String:\n" + inputString);
        System.out.println("\nOutput String:\n" + result);
    }
}
The output is:
Input String:
SIMPLE_SOLUTION

Output String:
simpleSolution

Happy Coding 😊