Dieser Artikel hat den spezifischen Code der Java -Kamelkonvertierung als Referenz geteilt. Der spezifische Inhalt ist wie folgt
Konvertieren Sie "_" in Kamel und konvertieren Sie das Kamel in "_".
Import Java.util.Regex.Matcher; Import Java.util.regex.Pattern; /*** Camel Conversion* @Author Hu Hansan* 19. Januar 2017 um 16:42:58 Uhr*/öffentliche Klasse Beanhump {// Konvertierte Abhängigkeit Charaktere öffentliche statische endgültige Zeichen Underline = '_'; / ** * Camel in "_" (userID: user_id) * @param param * @return */ public static String Cameltounderline (String Param) {if (param == null || "". Equals (param.trim ()) {return ""; } int len = param.length (); StringBuilder sb = new StringBuilder (len); für (int i = 0; i <len; i ++) {char c = param.charat (i); if (charakter.isuppercase (c)) {sb.append (unterstreich); sb.Append (charakter.tolowerCase (c)); } else {sb.append (c); }} return sb.toString (); } / ** * "_" in camel (user_id: userId) * @param param * @return * / public static String UnderLinetocamel (String Param) {if (param == null || "". Equals (param.trim ()) {return ""; } int len = param.length (); StringBuilder sb = new StringBuilder (len); für (int i = 0; i <len; i ++) {char c = param.charat (i); if (c == unterstreich) {if (++ i <len) {sb.append (Zeichen.ToUpperCase (param.charat (i))); }} else {sb.append (c); }} return sb.toString (); } / ** * konvertieren "_" in camel (user_id: userId) * @param param * @return * / public static String UnderLinetocamel2 (String Param) {if (param == null || "". Equals (param.trim ()) {return ""; } StringBuilder sb = new StringBuilder (param); Matcher MC = musters.comPile (unterstreicht+""). Matcher (Param); int i = 0; while (mc.find ()) {int Position = mc.end ()-(i ++); String.ValueOf (Zeichen.ToUpperCase (SB.Charat (Position))); Sb.Replace (Position-1, Position+1, Sb.Substring (Position, Position+1) .ToUppercase ()); } return sb.toString (); } /** Test* / public static void main (String [] args) {System.out.println (cameltounderline ("usernameAll"); System.out.println (UnderLinetocamel ("user_name_all")); System.out.println (UnderLinetocamel2 ("user_name_all")); }}Auslaufergebnisse:
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.