Keywords: Java String Conversion | CamelCase | Google Guava
Abstract: This paper comprehensively examines multiple approaches for converting underscore-separated uppercase strings to CamelCase format in Java. The Google Guava CaseFormat utility class is highlighted as the optimal solution due to its conciseness and efficiency. Comparative analysis with Apache Commons Lang and manual implementation methods provides detailed insights into implementation principles, performance characteristics, and application scenarios. Complete code examples and performance evaluations offer comprehensive technical guidance for developers.
Problem Background and Requirements Analysis
In Java development practice, there is a frequent need to convert underscore-separated uppercase strings (such as "THIS_IS_AN_EXAMPLE_STRING") to CamelCase format (like "ThisIsAnExampleString"). This conversion is particularly common in areas such as API design, database field mapping, and configuration file processing. While traditional string processing methods are feasible, they often result in verbose code that is prone to errors.
Google Guava Solution: The Optimal Choice
The <code>CaseFormat</code> class provided by the Google Guava library represents the optimal solution for this problem. Specifically designed for converting between different naming conventions, its core advantages include:
import com.google.common.base.CaseFormat;
public class StringConverter {
public static String toCamelCase(String input) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, input);
}
// Test case
public static void main(String[] args) {
String result = toCamelCase("THIS_IS_AN_EXAMPLE_STRING");
System.out.println(result); // Output: ThisIsAnExampleString
}
}
The implementation core relies on Guava's predefined format mapping rules. <code>CaseFormat.UPPER_UNDERSCORE</code> recognizes the underscore-separated pattern in the input string, while <code>CaseFormat.UPPER_CAMEL</code> specifies the target CamelCase format. The conversion process automatically handles these key steps:
- String splitting: Divides the input string into word arrays based on underscore characters
- Case conversion: Capitalizes the first letter of each word and converts remaining letters to lowercase
- String concatenation: Removes all separators and joins processed words into the final result
Apache Commons Lang Alternative
The Apache Commons Lang library offers another viable solution using the <code>WordUtils.capitalizeFully()</code> method:
import org.apache.commons.text.WordUtils;
public class CommonsConverter {
public static String toCamelCase(String input) {
String capitalized = WordUtils.capitalizeFully(input, '_');
return capitalized.replace("_", "");
}
}
This method first converts each word separated by underscores to a format with the first letter capitalized and the rest in lowercase, then removes all underscores via the <code>replace()</code> method. While functionally complete, it requires additional string replacement operations and depends on specific methods from external libraries.
Manual Implementation Approach
For projects that prefer to avoid external dependencies, a pure Java implementation can be employed:
public class ManualConverter {
public static String toCamelCase(String input) {
if (input == null || input.isEmpty()) {
return input;
}
String[] parts = input.split("_");
StringBuilder result = new StringBuilder();
for (String part : parts) {
if (!part.isEmpty()) {
result.append(Character.toUpperCase(part.charAt(0)));
if (part.length() > 1) {
result.append(part.substring(1).toLowerCase());
}
}
}
return result.toString();
}
}
This implementation clearly demonstrates each step of the conversion process: string splitting, first-letter capitalization, remaining letter case conversion, and final result concatenation. Using <code>StringBuilder</code> avoids the performance overhead associated with string concatenation.
Performance Analysis and Best Practices
Through performance testing and code analysis of the three approaches, the following conclusions can be drawn:
- Google Guava Solution: Optimal performance, most concise code, suitable for most production environments
- Apache Commons Solution: Functionally complete but incurs additional string replacement overhead
- Manual Implementation: No external dependencies, easy to understand and customize, but requires handling edge cases manually
When selecting an approach for actual projects, consider the project's dependency management strategy, performance requirements, and team technology stack preferences. For new projects, the Google Guava solution is recommended; for projects with existing Apache Commons dependencies, the corresponding solution can be chosen; for lightweight applications or learning purposes, the manual implementation offers the best comprehensibility.
Extended Applications and Considerations
Beyond basic underscore-to-upper-camel conversion, these methods can be extended to other naming format conversions, such as lower camel case, hyphen-separated formats, etc. In practical use, attention must be paid to handling edge cases including empty strings, null values, and strings containing numbers or special characters.
By appropriately selecting conversion methods, developers can significantly improve code readability and maintainability while ensuring efficient and correct string processing.