What is the most elegant way to convert a hyphen separated word (e.g. “do-some-stuff”) to the lower camel-case variation (e.g. “doSomeStuff”)?

后端 未结 11 909
清歌不尽
清歌不尽 2020-11-28 07:16

What is the most elegant way to convert a hyphen separated word (e.g. \"do-some-stuff\") to the lower camel-case variation (e.g. \"doSomeStuff\") in Java?

相关标签:
11条回答
  • 2020-11-28 07:58

    With Java 8 there is finally a one-liner:

    Arrays.stream(name.split("\\-"))
        .map(s -> Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase())
        .collect(Collectors.joining());
    

    Though it takes splitting over 3 actual lines to be legible ツ

    (Note: "\\-" is for kebab-case as per question, for snake_case simply change to "_")

    0 讨论(0)
  • 2020-11-28 07:58

    For those who has com.fasterxml.jackson library in the project and don't want to add guava you can use the jaskson namingStrategy method:

    new PropertyNamingStrategy.SnakeCaseStrategy.translate(String);
    
    0 讨论(0)
  • 2020-11-28 07:59

    Here is a slight variation of Andreas' answer that does more than the OP asked for:

    public static String toJavaMethodName(final String nonJavaMethodName){
        final StringBuilder nameBuilder = new StringBuilder();
        boolean capitalizeNextChar = false;
        boolean first = true;
    
        for(int i = 0; i < nonJavaMethodName.length(); i++){
            final char c = nonJavaMethodName.charAt(i);
            if(!Character.isLetterOrDigit(c)){
                if(!first){
                    capitalizeNextChar = true;
                }
            } else{
                nameBuilder.append(capitalizeNextChar
                    ? Character.toUpperCase(c)
                    : Character.toLowerCase(c));
                capitalizeNextChar = false;
                first = false;
            }
        }
        return nameBuilder.toString();
    }
    

    It handles a few special cases:

    • fUnnY-cASe is converted to funnyCase
    • --dash-before-and--after- is converted to dashBeforeAndAfter
    • some.other$funky:chars? is converted to someOtherFunkyChars
    0 讨论(0)
  • 2020-11-28 08:03

    As I'm not a big fan of adding a library just for one method, I implemented my own solution (from camel case to snake case):

    public String toSnakeCase(String name) {
        StringBuilder buffer = new StringBuilder();
        for(int i = 0; i < name.length(); i++) {
            if(Character.isUpperCase(name.charAt(i))) {
                if(i > 0) {
                    buffer.append('_');
                }
                buffer.append(Character.toLowerCase(name.charAt(i)));
            } else {
                buffer.append(name.charAt(i));
            }
        }
        return buffer.toString();
    }
    

    Needs to be adapted depending of the in / out cases.

    0 讨论(0)
  • 2020-11-28 08:04

    get The Apache commons jar for StringUtils. Then you can use the capitalize method

    import org.apache.commons.lang.StringUtils;
    public class MyClass{
    
        public String myMethod(String str) {
            StringBuffer buff = new StringBuffer();
    
            String[] tokens = str.split("-");
            for (String i : tokens) {
                buff.append(StringUtils.capitalize(i));
            }
    
            return buff.toString();
        }
    }
    
    0 讨论(0)
提交回复
热议问题