"Original" String tool class--hump method and underline method of mutual transfer

Source: Internet
Author: User

In the actual project development, will encounter such a problem, the database table structure is well designed, but the entity class has not been developed accordingly. The property naming method of entity class is usually hump method, and the method of naming table field in database is using underscore method. If the table has a lot of fields, it is a bit time-consuming to write the properties of the hump again manually, based on the database fields that were designed. How to quickly change the table fields in the database into the camel-like properties we need?

The solution has two, one is through the text editing tools, such as editplus,notepad++, and so on, using the regular replacement function they carry to quickly achieve, and the second is to write their own tool class to achieve. As for the first method of operation skill, do not repeat here. The second method is as follows:

Here is the code for the tool class you wrote:

 Packageday0704;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/*** Hump Method-Underline cross-turn *@authorCshaper *@since2015.07.04 *@version1.0.0*/ Public classUnderline2camel {/*** Underline Turn Hump method *@paramLine Source String *@paramSmallcamel Size Hump, whether for small hump *@returnconverted string*/     Public StaticString Underline2camel (String line,BooleanSmallcamel) {        if(line==NULL||"". Equals (line)) {            return""; } stringbuffer SB=NewStringBuffer (); 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))); intIndex=word.lastindexof (' _ ')); if(index>0) {sb.append (word.substring (1, index). toLowerCase ()); }Else{sb.append (word.substring (1). toLowerCase ()); }        }        returnsb.tostring (); }    /*** Hump method turns underline *@paramLine Source String *@returnconverted string*/     Public Staticstring Camel2underline (string line) {if(line==NULL||"". Equals (line)) {            return""; } Line=string.valueof (Line.charat (0)). toUpperCase (). Concat (line.substring (1)); StringBuffer SB=NewStringBuffer (); 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 ()? "": "_"); }        returnsb.tostring (); }     Public Static voidMain (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 of the operation are as follows:

Ihaveanipang3pig
I_have_an_ipang3_pig

"Original" String tool class--hump method and underline method of mutual transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.