驼峰与下划线之间的转换

驼峰与下划线之间的转换

/**
 * 
 */
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 data= new HashMap();
		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 camelMap(Map data) {
		if (data == null) {
			return null;
		}
		Map ret = new HashMap<>();
		Iterator 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 underscopeMap(Map data) {
		if (data == null) {
			return null;
		}
		Map ret = new HashMap<>();
		Iterator 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;
	}

}

  

转载于:https://www.cnblogs.com/xiangpeng/p/10189390.html

你可能感兴趣的:(驼峰与下划线之间的转换)