CharUtils.java 3.71 KB
package com.yoho.datasync.producer.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharUtils {
	/**
	 * 将字符转换成大写,如a->A
	 * 
	 * @param c
	 * @return
	 */
	public static char toUpperCase(char c) {
		if (c < 97 || c > 122)
			return c;
		else
			return (char) (c - 32);
	}

	/**
	 * 将字符转换成小写,如A->a
	 * 
	 * @param c
	 * @return
	 */
	public static char toLowerCase(char c) {
		if (c < 65 || c > 90)
			return c;
		else
			return (char) (c + 32);
	}

	public static boolean isLowerCase(char c) {
		return (c >= 'a' && c <= 'z');
	}

	public static boolean isUpperCase(char c) {
		return (c >= 'A' && c <= 'Z');
	}

	public static String getLowerCaseNames(String names) {
		StringBuilder sb = new StringBuilder();
		char[] cc = names.toCharArray();
		sb.append(toLowerCase(cc[0]));
		for (int i = 1; i < cc.length; i++) {
			if (isLowerCase(cc[i - 1]) && isUpperCase(cc[i])) {
				sb.append("_" + toLowerCase(cc[i]));
			} else {
				sb.append(toLowerCase(cc[i]));
			}
		}
		;
		return sb.toString();
	}

	/**
	 * 将驼峰风格替换为下划线风格
	 * 
	 * @param str
	 * @return
	 */
	public static String CamelhumpToUnderline(String str) {
		Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
		StringBuilder builder = new StringBuilder(str);
		for (int i = 0; matcher.find(); i++) {
			builder.replace(matcher.start() + i, matcher.end() + i, "_" + matcher.group().toLowerCase());
		}
		if (builder.charAt(0) == '_') {
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}

	/**
	 * 将下划线风格替换为驼峰风格
	 * 
	 * @param str
	 * @return
	 */
	public static String underlineToCamelhump(String str) {
		Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
		StringBuilder builder = new StringBuilder(str);
		for (int i = 0; matcher.find(); i++) {
			builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
		}
		if (Character.isUpperCase(builder.charAt(0))) {
			builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
		}
		return builder.toString();
	}
	
	public static String underlineToCamelhumpWithNumber(String str) {
        Matcher matcher = Pattern.compile("_[a-z0-9]").matcher(str);
        StringBuilder builder = new StringBuilder(str);
        for (int i = 0; matcher.find(); i++) {
            builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
        }
        if (Character.isUpperCase(builder.charAt(0))) {
            builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
        }
        return builder.toString();
	}

	public static String getBeanName(String str) {
		StringBuilder builder = new StringBuilder(str);
		builder.replace(0, 1, String.valueOf(Character.toUpperCase(builder.charAt(0))));
		return builder.toString();
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	public static String standardized(String str){
		if(StringUtils.isEmpty(str)){
			return str;
		}

		return str.trim().toLowerCase().replaceAll(" ", "");
	}
	
	public static void main(String[] args) {
		System.out.println(isLowerCase('C'));
		
		String s1 = "size_attribute";
		
		System.out.println(underlineToCamelhump(s1));
		
		String s2 = "hserInfo";
		System.out.println(getLowerCaseNames(s2));

		String s3 = "brand";
		System.out.println(getBeanName(s3));
		
		String s4 = "skn_7_days_view";
        System.out.println(underlineToCamelhumpWithNumber(s4));

		System.out.println(standardized("Vans 卫衣"));
	}
}