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 944
清歌不尽
清歌不尽 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 "_")

提交回复
热议问题