In java the hump turns on the underlining

  • 2020-05-30 20:06:06
  • OfStack

preface

In the actual project development, will encounter such a problem, the database table structure is designed, but the entity class has not been out accordingly. The attribute naming method of entity class 1 is generally the hump method, while the table field naming method in the database USES the underscore method. If there are too many fields in the table, we will manually type and write the attributes of the hump method 1 time according to the database field designed, which is a little time consuming. How do you quickly turn table fields in a database into the hump properties we need?

There are 2,1 is through text editing tools, such as EditPlus,Notepad++, using the regular replacement function they carry to achieve quickly; 2 is implemented by writing your own tool classes. As for the operation skills of the first method, I will not repeat them here.

The second method is as follows:

Here is the code of the tool class I wrote:


package day0704;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  Hump method - The underlining goes around 
 * @author cshaper
 * @since 2015.07.04
 * @version 1.0.0
 */
public class Underline2Camel {
 
 /**
  *  Underline to hump 
  * @param line  The source string 
  * @param smallCamel  The size of the hump , Whether it is a small hump 
  * @return  The converted string 
  */
 public static String underline2Camel(String line,boolean smallCamel){
  if(line==null||"".equals(line)){
   return "";
  }
  StringBuffer sb=new StringBuffer();
  Pattern pattern=Pattern.compile("([A-Za-z\\d]+)(_)?");
  Matcher matcher=pattern.matcher(line);
  while(matcher.find()){
   String word=matcher.group();
   sb.append(smallCamel&&matcher.start()==0?Character.toLowerCase(word.charAt(0)):Character.toUpperCase(word.charAt(0)));
   int index=word.lastIndexOf('_');
   if(index>0){
    sb.append(word.substring(1, index).toLowerCase());
   }else{
    sb.append(word.substring(1).toLowerCase());
   }
  }
  return sb.toString();
 }
 /**
  *  The hump is underlined 
  * @param line  The source string 
  * @return  The converted string 
  */
 public static String camel2Underline(String line){
  if(line==null||"".equals(line)){
   return "";
  }
  line=String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
  StringBuffer sb=new StringBuffer();
  Pattern pattern=Pattern.compile("[A-Z]([a-z\\d]+)?");
  Matcher matcher=pattern.matcher(line);
  while(matcher.find()){
   String word=matcher.group();
   sb.append(word.toUpperCase());
   sb.append(matcher.end()==line.length()?"":"_");
  }
  return sb.toString();
 }
 public static void main(String[] args) {
  String line="I_HAVE_AN_IPANG3_PIG";
  String camel=underline2Camel(line,true);
  System.out.println(camel);
  System.out.println(camel2Underline(camel));
 }
}

The results are as follows:

iHaveAnIpang3Pig

I_HAVE_AN_IPANG3_PIG

conclusion

The above is the whole content of this article, I hope the content of this article for everyone to learn or use Java can have 1 definite help, if you have any questions, you can leave a message to communicate.


Related articles: