1 /**2 * Converts a camel-named string to an underscore capitalization method. An empty string is returned if the pre-converted camel-named string is empty. </br>3 * For example: Helloworld->hello_world4 * @paramName the camel-named string before the conversion5 * @returnA string named after the converted underscore capitalization6 */7 Public Staticstring Underscorename (string name) {8StringBuilder result =NewStringBuilder ();9 if(Name! =NULL&& name.length () > 0) {Ten //process the first character into uppercase OneResult.append (name.substring (0, 1). toUpperCase ()); A //Loop through the remaining characters - for(inti = 1; I < name.length (); i++) { -String s = name.substring (i, i + 1); the //underline before capital letters - if(S.equals (S.touppercase ()) &&! Character.isdigit (S.charat (0))) { -Result.append ("_"); - } + //other characters go directly to uppercase - Result.append (S.touppercase ()); + } A } at returnresult.tostring (); - } - - /** - * Converts a string named by an underscore in uppercase to a camel-style. Returns an empty string if the string that is named before the underscore capitalization method is empty. </br> - * For example: Hello_world->helloworld in * @paramname the string that is named before the underscore capitalization method - * @returnconverted camel-named string to */ + Public Staticstring Camelname (string name) { -StringBuilder result =NewStringBuilder (); the //Quick Check * if(Name = =NULL||Name.isempty ()) { $ //no need to convertPanax Notoginseng return""; -}Else if(!name.contains ("_")) { the //does not contain underscores, lowercase only the first letter + returnName.substring (0, 1). toLowerCase () + name.substring (1); A } the //split the original string with an underscore +String camels[] = Name.split ("_"); - for(String camel:camels) { $ //skips the beginning, end of a line change, or double underline in the original string $ if(Camel.isempty ()) { - Continue; - } the //handle Real hump fragments. - if(result.length () = = 0) {Wuyi //First Hump fragment, all letters lowercase the Result.append (Camel.tolowercase ()); -}Else { Wu //Other hump fragments, capitalized in the first letter -Result.append (camel.substring (0, 1). toUpperCase ()); AboutResult.append (camel.substring (1). toLowerCase ()); $ } - } - returnresult.tostring (); -}
Camel style, underline style, cross-turn