• 驼峰与下划线之间的转换


    驼峰与下划线之间的转换

    /**
     * 
     */
    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;
    	}
    
    }
    

      

  • 相关阅读:
    javascript零散要点收集
    javascript闭包,arguments和prototype
    javascript面向对象规则汇总以及json
    递归转非递归的编程思想
    汇编要点汇总
    队列相关算法
    深度优先遍历算法
    C++面向对象要点
    堆排序
    快速排序算法
  • 原文地址:https://www.cnblogs.com/xiangpeng/p/10189390.html
Copyright © 2020-2023  润新知