How to Convert a Java String from Uppercase with Underscores to CamelCase?

Question

How can I transform a Java string from all uppercase with underscores to CamelCase?

String input = "THIS_IS_AN_EXAMPLE_STRING";

Answer

Converting a Java string from uppercase with underscores to CamelCase involves replacing underscores with empty strings and capitalizing the letters that follow them. This can be achieved efficiently using regular expressions in conjunction with Java string methods.

String input = "THIS_IS_AN_EXAMPLE_STRING";
String camelCaseString = input.toLowerCase()
                     .replaceAll("(?:^|_)(\w)", match -> match.group(1).toUpperCase());
System.out.println(camelCaseString); // Outputs: ThisIsAnExampleString

Causes

  • The string is initially in uppercase with underscores separating the words.
  • We need to create a format where each word starts with an uppercase letter and there are no separators.

Solutions

  • Use `String.replaceAll()` to find underscores and the subsequent character, and replace them with the uppercase version of that character.
  • Alternatively, use `String.split()` to divide the string and then manually construct the CamelCase result.

Common Mistakes

Mistake: Forgetting to handle the first word which does not follow an underscore.

Solution: Ensure to convert the first letter of the string after transforming the string to lower case.

Mistake: Not using the right regex pattern to match underscores and capitalize correctly.

Solution: Double-check the regex and use proper capturing groups in your replacement.

Helpers

  • Java string manipulation
  • convert string to CamelCase
  • Java regex examples
  • camelCase conversion in Java

Related Questions

⦿How to Convert a String to SHA-1 in Java

Learn how to correctly convert a string to SHA1 hash in Java with a complete example and common pitfalls to avoid.

⦿How to Encrypt Strings in Java for Barcode Security

Learn how to easily encrypt strings in Java for 2D barcodes like PDF417 ensuring secure data transmission without complex setups.

⦿How to Parse a String with Comma as Decimal Separator in Java?

Learn the best method to parse a string with a comma as a decimal separator in Java without causing NumberFormatException.

⦿Why is the @Column Annotation Ignored in Spring Boot JPA?

Understand why the Column annotation may be ignored in Spring Boot JPA especially when dealing with SQL Server dialects.

⦿How to Copy Files from One Directory to Another in Java

Learn how to copy files between directories in Java using File and Files class for efficient file management.

⦿How to Fix 'Could Not Initialize Plugin: interface org.mockito.plugins.MockMaker' Exception in Mockito

Learn how to resolve the Could not initialize plugin interface org.mockito.plugins.MockMaker exception in Mockito effectively.

⦿How to Properly Use Java -D Command-Line Parameters

Learn the correct usage of Java D commandline parameters and how to access them in your code to avoid NullPointerException.

⦿How to Properly Escape Strings for JSON in Java?

Learn the best practices for escaping strings in JSON data in Java. Understand when to use specific libraries and avoid common pitfalls.

⦿How to Add Elements to a List of Type ? extends Number in Java

Learn how to properly handle List extends Number in Java including how to add elements and avoid common errors.

⦿How to Represent Infinity in Java for Different Data Types?

Learn how to implement and work with infinity in Java using different numerical data types for your mathematical operations.

© Copyright 2025 - CodingTechRoom.com