Home > Software design >  Convert a String to customised version of Snake case and capitalise first character before every und
Convert a String to customised version of Snake case and capitalise first character before every und

Time:09-09

Suppose I have string like FOO_BAR or foo_bar or Foo_Bar, I want to convert it to customised snake case like below

 FOO_BAR ->   Foo Bar
`foo_bar` -> `Foo Bar`
`Foo_Bar` -> `Foo Bar`

As you can notice all three inputs provide the same output, capitalise the first character before every underscore and first one, and remove the _ (underscore).

Looked into various libraries like guava and apache but as this is customized version couldn't find any Out of the box solution.

Tried below code but and make it work but its looking bit complex

str.replaceAll("([a-z])([A-Z])", "$1_$2").replaceAll("_", " ")

Output of above code is like FOO BAR basically all characters in uppercase, that i can fix in another iteration but looking for something more efficient and simple.

CodePudding user response:

Just for a bit of fun, here is a stream-based answer:

var answer = Arrays.stream(s.split("_"))
             .map(i -> i.substring(0, 1).toUpperCase()   i.substring(1).toLowerCase())
             .collect(Collectors.joining(" "));

CodePudding user response:

Here's a simple implementation. I would add a few more test cases before I trusted it. It does not handle Unicode characters of more than two bytes.

public class Snakify {
    public static String toSnake(String in) {
        boolean first = true;
        boolean afterUnderscore = false;
        char[] chars = in.toCharArray();
        for (int i = 0; i < chars.length; i  ) {
            if ((first || afterUnderscore) && Character.isAlphabetic(chars[i])) {
                chars[i] = Character.toUpperCase(chars[i]);
                first = false;
                afterUnderscore = false;
            } else if (chars[i] == '_') {
                chars[i] = ' ';
                afterUnderscore = true;
            } else if (Character.isAlphabetic(chars[i])) {
                chars[i] = Character.toLowerCase(chars[i]);
            }
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        System.out.println(toSnake("FOO_BAR").equals("Foo Bar"));
        System.out.println(toSnake("foo_bar").equals("Foo Bar"));
        System.out.println(toSnake("Foo_Bar").equals("Foo Bar"));
        System.out.println(toSnake("àèì_òù_ÀÈÌ").equals("Àèì Òù Àèì"));
    }
}
  • Related