驼峰与下划线互相转换(JS + JAVA)

Lewis
2022-07-08 / 0 评论 / 22 阅读 / 正在检测是否收录...

/**
 * 将驼峰转为下划线
 */
function camelToUnderline(camelStr){
    return camelStr.replace(/[A-Z]/g,function(s){
        return ' '+s.toLowerCase();
    }).trim().replaceAll(' ','_');
}

/**
 * 下划线转小驼峰
 */
function underlineToSmallCamel(str){
    return str.toLowerCase().replace(/_([a-z])/g,function(s, s1){
        return s1.toUpperCase();
    })
}

/**
 * 华氏温度(32℉) 转化为摄氏温度(0℃)
 */
function f2c(s) {
  return s.replace(/(\d+(\.\d*)?)℉/g, function($0,$1,$2) {
      return (($1-32) * 5/9) + '℃';
  });
}

/**
 *
 */
package com;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
/**
 * @author xp test5.java 2018年12月27日
 */
public class test5 {
    public static void main(String[] args) {
        //underscoreName("abcAbcaBc");
        //camelName("abc_abca_bc");
        //upcaseCamelName("abc_abca_bc");
        Map<String, Object> data= new HashMap<String, Object>();
        data.put("Abc_Abca_Bc_TEXT", "");
        camelMap(data);
        //underscopeMap(data);
    }
     
    //驼峰转大写+下划线,abcAbcaBc->ABC_ABCA_BC
    public static String underscoreName(String name) {
        StringBuilder result = new StringBuilder();
        if ((name != null) && (name.length() > 0)) {
            result.append(name.substring(0, 1).toUpperCase());
            for (int i = 1; i < name.length(); i++) {
                String s = name.substring(i, i + 1);
                if ((s.equals(s.toUpperCase())) && (!Character.isDigit(s.charAt(0)))) {
                    result.append("_");
                }
                result.append(s.toUpperCase());
            }
        }
        System.err.println("underscoreName:"+result.toString());
        return result.toString();
    }
     
    //下划线转驼峰,abc_abca_bc->abcAbcaBc
    public static String camelName(String name) {
        StringBuilder result = new StringBuilder();
        if ((name == null) || (name.isEmpty())) {
            return "";
        }
        if (!name.contains("_")) {
            return name.toLowerCase();
        }
        String[] camels = name.split("_");
        for (String camel : camels) {
            if (!camel.isEmpty()) {
                if (result.length() == 0) {
                    result.append(camel.toLowerCase());
                } else {
                    result.append(camel.substring(0, 1).toUpperCase());
                    result.append(camel.substring(1).toLowerCase());
                }
            }
        }
        System.err.println("camelName:"+result.toString());
        return result.toString();
    }
 
    //下划线转首字母大写驼峰,abc_abca_bc->AbcAbcaBc
    public static String upcaseCamelName(String name) {
        StringBuilder result = new StringBuilder();
        if ((name == null) || (name.isEmpty())) {
            return "";
        }
        if (!name.contains("_")) {
            result.append(name.substring(0, 1).toUpperCase());
            result.append(name.substring(1).toLowerCase());
            return result.toString();
        }
        String[] camels = name.split("_");
        for (String camel : camels) {
            if (!camel.isEmpty()) {
                result.append(camel.substring(0, 1).toUpperCase());
                result.append(camel.substring(1).toLowerCase());
            }
        }
        System.err.println("upcaseCamelName:"+result.toString());
        return result.toString();
    }
 
    public static Map<String, Object> camelMap(Map<String, Object> data) {
        if (data == null) {
            return null;
        }
        Map<String, Object> ret = new HashMap<>();
        Iterator<String> keyIt = data.keySet().iterator();
        while (keyIt.hasNext()) {
            String key = (String) keyIt.next();
            ret.put(camelName(key), data.get(key));
            if (key.endsWith("_TEXT")) {
                String key1 = key.substring(0, key.lastIndexOf("_"));
                ret.put(camelName(key1) + "_Text", data.get(key));
            }
        }
        System.err.println("data:"+data);
        System.err.println("camelMap:"+ret);
        return ret;
    }
 
    public static Map<String, Object> underscopeMap(Map<String, Object> data) {
        if (data == null) {
            return null;
        }
        Map<String, Object> ret = new HashMap<>();
        Iterator<String> keyIt = data.keySet().iterator();
        while (keyIt.hasNext()) {
            String key = (String) keyIt.next();
            ret.put(underscoreName(key), data.get(key));
        }
        System.err.println("underscopeMap:"+ret);
        return ret;
    }
 
}
0

评论 (0)

取消