This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Actual combat tips 5: hump and underline turn each other

A daily practical tip: swivel the hump with the underscore

This is a very useful question, especially for those of us who claim to only need CURD for back-end development. Humps and underscores are part of everyday tasks. In general, column names in DB should be in underline format (why? Ali database specification is so defined, as I feel the hump is not wrong), and The Java entity name is the hump format, so between them, there must be a hump and underline

Today we’re going to look at how these two support each other

1. Gauva

In general, when faced with this kind of universality problem, most of them have ready-made utility classes that can be used directly; In the Java ecosystem, Guava is near the top of the list when it comes to handy tools

Now let’s see how we can use Gauva for our purposes

// The hump is underlined
String ans = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "helloWorld");
System.out.println(ans);

// The underline turns the hump
String ans2 = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "hello_world");
System.out.println(ans2);
Copy the code

CaseFormat is mainly used here to implement inter-conversion, but Guava’s CaseFormat provides several other ways

The one above can translate, but if we have a string called helloWorld_Case

The output of other transformations is as follows:

  • The underline:hello_world__case
  • Hump:helloworldCase

This output is not quite the same as the standard hump/underline (of course because the input is also not standard)

2. Hutool

In addition to the guava above, huTool is widely used, including many tool classes, and StrUtil provides support for underlining and humping

String ans = StrUtil.toCamelCase("hello_world");
System.out.println(ans);
String ans2 = StrUtil.toUnderlineCase("helloWorld");
System.out.println(ans2);
Copy the code

Again, let’s look at the special case

System.out.println(StrUtil.toCamelCase("helloWorld_Case"));
System.out.println(StrUtil.toUnderlineCase("helloWorld_Case"));
Copy the code

The output is as follows

  • Hump:helloworldCase
  • The underline:hello_world_case

Compared to guava’s scene above, the underlined one looks fine

3. Custom implementation

Next, in order to meet the needs of the hump/underline output that we want to convert to a standard brick, we’ll do one ourselves

Underline turn camel peak:

  • The key is to find the underscore, remove it, and capitalize the next character (if it’s still an underscore, move on to the next one).

According to the above idea to implement, as follows

private static final char UNDER_LINE = '_';

/** ** *@param name
 * @return* /
public static String toCamelCase(String name) {
    if (null == name || name.length() == 0) {
        return null;
    }

    int length = name.length();
    StringBuilder sb = new StringBuilder(length);
    boolean underLineNextChar = false;

    for (int i = 0; i < length; ++i) {
        char c = name.charAt(i);
        if (c == UNDER_LINE) {
            underLineNextChar = true;
        } else if (underLineNextChar) {
            sb.append(Character.toUpperCase(c));
            underLineNextChar = false;
        } else{ sb.append(c); }}return sb.toString();
}
Copy the code

The hump is underlined

  • Key: if it is uppercase, add an underscore before it and turn the current character to lowercase (if it is already an underscore, then do not add the preceding character and turn it to lowercase)
public static String toUnderCase(String name) {
    if (name == null) {
        return null;
    }

    int len = name.length();
    StringBuilder res = new StringBuilder(len + 2);
    char pre = 0;
    for (int i = 0; i < len; i++) {
        char ch = name.charAt(i);
        if (Character.isUpperCase(ch)) {
            if(pre ! = UNDER_LINE) { res.append(UNDER_LINE); } res.append(Character.toLowerCase(ch)); }else {
            res.append(ch);
        }
        pre = ch;
    }
    return res.toString();
}
Copy the code

Test helloWorld_Case again and the output is as follows

  • Hump:helloWorldCase
  • The underline:hello_world_case

Series of blog posts

  • Practical tips 1: string placeholder replacement -JDK version
  • Practical tips 2: Array and list inter-transfer
  • Practical tip 3: String and container interchange
  • Actual combat tips 4: elegant implementation string concatenation

II. The other

1. A gray Blog:liuyueyi.github.io/hexblog

A gray personal blog, record all study and work in the blog, welcome everyone to visit

2. Statement

The above content is not as good as the letter, purely the words of a family, due to the limited personal ability, there are inevitably omissions and mistakes, such as found bugs or better suggestions, welcome criticism and correction, not grudging gratitude

  • Micro-blog address: small gray Blog
  • QQ: A gray /3302797840
  • Wechat official account: a gray blog