What Is the Simplest Way to Convert a Java String from All Caps (Words Separated by Underscores) to Camelcase (No Word Separators)

What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

Another option is using Google Guava's com.google.common.base.CaseFormat

George Hawkins left a comment with this example of usage:

CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "THIS_IS_AN_EXAMPLE_STRING");

What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

Another option is using Google Guava's com.google.common.base.CaseFormat

George Hawkins left a comment with this example of usage:

CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "THIS_IS_AN_EXAMPLE_STRING");

Convert a String to Modified Camel Case in Java or Title Case as is otherwise called

I used the below to solve this problem.

import org.apache.commons.lang.StringUtils;
StringUtils.capitalize(MyString);

Thanks to Ted Hopp for rightly pointing out that the question should have been TITLE CASE instead of modified CAMEL CASE.

Camel Case is usually without spaces between words.

How to modify this String in Java?

Combine your partial solution, with the function described here:

What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

Converting a string from snake case to camel case in Java

Guava supports this through its CaseFormat class

import com.google.common.base.CaseFormat;

public class StackOverflow25680258 {

public static void main(String[] args) {
System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "input_in_snake_case"));
}

}

Output

InputInSnakeCase

Regex for converting CamelCase to camel_case in java

See this question and CaseFormat from guava

in your case, something like:

CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "SomeInput");

How do I convert CamelCase into human-readable names in Java?

This works with your testcases:

static String splitCamelCase(String s) {
return s.replaceAll(
String.format("%s|%s|%s",
"(?<=[A-Z])(?=[A-Z][a-z])",
"(?<=[^A-Z])(?=[A-Z])",
"(?<=[A-Za-z])(?=[^A-Za-z])"
),
" "
);
}

Here's a test harness:

    String[] tests = {
"lowercase", // [lowercase]
"Class", // [Class]
"MyClass", // [My Class]
"HTML", // [HTML]
"PDFLoader", // [PDF Loader]
"AString", // [A String]
"SimpleXMLParser", // [Simple XML Parser]
"GL11Version", // [GL 11 Version]
"99Bottles", // [99 Bottles]
"May5", // [May 5]
"BFG9000", // [BFG 9000]
};
for (String test : tests) {
System.out.println("[" + splitCamelCase(test) + "]");
}

It uses zero-length matching regex with lookbehind and lookforward to find where to insert spaces. Basically there are 3 patterns, and I use String.format to put them together to make it more readable.

The three patterns are:

UC behind me, UC followed by LC in front of me

  XMLParser   AString    PDFLoader
/\ /\ /\

non-UC behind me, UC in front of me

 MyClass   99Bottles
/\ /\

Letter behind me, non-letter in front of me

 GL11    May5    BFG9000
/\ /\ /\

References

  • regular-expressions.info/Lookarounds

Related questions

Using zero-length matching lookarounds to split:

  • Regex split string but keep separators
  • Java split is eating my characters

Converting any string into camel case

I just ended up doing this:

String.prototype.toCamelCase = function(str) {
return str
.replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
.replace(/\s/g, '')
.replace(/^(.)/, function($1) { return $1.toLowerCase(); });
}

I was trying to avoid chaining together multiple replace statements. Something where I'd have $1, $2, $3 in my function. But that type of grouping is hard to understand, and your mention about cross browser problems is something I never thought about as well.

Matching a lower-case character on the position before an uppercase character (camelCase)?

You can use

const splitRegex: RegExp = /[-_\s]|([a-z](?=[A-Z]))/;

Details:

  • [-_\s] - a character class matching a -, _ or a whitespace
  • | - or
  • ([a-z](?=[A-Z])) - a capturing group with ID=1 that matches a lowercase ASCII letter followed with an uppercase ASCII letter without adding the latter to the overall match value (as it is inside a positive lookahead that is a non-consuming regex construct).


Related Topics



Leave a reply



Submit