I have the string "B2BNewQuoteProcess". When I use Guava to convert from Camel Case to Lower Hyphen as follows:
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,"B2BNewQuoteProcess");
I get "b2-b-new-quote-process".
What I am looking for is "b2b-new-quote-process"...
How do I do this in Java?
To prevent -
at the beginning of a line, use the following instead of my original answer:
(?!^)(?=[A-Z][a-z])
See regex in use here
(?=[A-Z][a-z])
Replacement: -
Note: The regex above doesn't convert the uppercase characters to lowercase; it simply inserts -
into the positions that should have them. The conversion of uppercase characters to lowercase character occurs in the Java code below using .toLowerCase()
.
See code in use here
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
final String regex = "(?=[A-Z][a-z])";
final String string = "B2BNewQuoteProcess";
final String subst = "-";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result.toLowerCase());
}
}
(?=[A-Z][a-z])
Positive lookahead ensuring what follows is an uppercase ASCII letter followed by a lowercase ASCII letter. This is used as an assertion for the position. The replacement simply inserts a hyphen -
into the positions that match this lookahead.Camel case | Lower hyphen | Should have been |
---|---|---|
B2BNewQuoteProcess | b2-b-new-quote-process | b2b-new-quote-process |
BaBNewQuoteProcess | ba-b-new-quote-process | |
B2NewQuoteProcess | b2-new-quote-process | |
BABNewQuoteProcess | b-a-b-new-quote-process | bab-new-quote-process |
So:
A repair of the wrong result would be:
String expr = "b2-b-new-quote-process";
expr = Pattern.compile("\\b[a-z]\\d*(-[a-z]\\d*)+\\b")
.matcher(expr).replaceAll(mr ->
mr.group().replace("-", ""));
This searches between word boundaries (\b
) a sequence of letter with any digits, followed by a repetition of hyphen plus letter with any digits.
METHOD FOR VERSIONS BELOW JAVA 8
Use this method to convert any camel case string. You can select any type of separator.
private String camelCaseToLowerHyphen(String s) {
StringBuilder parsedString = new StringBuilder(s.substring(0, 1).toLowerCase());
for (char c : s.substring(1).toCharArray()) {
if (Character.isUpperCase(c)) {
parsedString.append("_").append(Character.toLowerCase(c));
} else {
parsedString.append(c);
}
}
return parsedString.toString().toLowerCase();
}
}
Original code taken from: http://www.java2s.com/example/java-utility-method/string-camel-to-hyphen-index-0.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With