String格式化工具类-StringFormatUtil
============================================================================String格式化工具类:
import java.util.Arrays; public class StringFormatUtil { /** * 打印异常信息 等同于 exception.printStackTrace(); * @param exception * @return */ public static synchronized String getStackTrace(Exception exception) { StringBuffer sb = new StringBuffer(); sb.append(exception.getClass()).append(": "); sb.append(exception.getMessage()); sb.append("\r\n"); for (StackTraceElement stackTraceElement : exception.getStackTrace()) { sb.append(" "); sb.append(stackTraceElement.toString()); sb.append("\r\n"); } return sb.toString(); } /** 驼峰命名法工具 * @return * camelCase("hello_world") == "helloWorld" * capCamelCase("hello_world") == "HelloWorld" * uncamelCase("helloWorld") = "hello_world" */ public static String camelCase(String s) { if (s == null) { return null; } s = s.toLowerCase(); StringBuilder sb = new StringBuilder(s.length()); boolean upperCase = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '_') { upperCase = true; } else if (upperCase) { sb.append(Character.toUpperCase(c)); upperCase = false; } else { sb.append(c); } } return sb.toString(); } /** 驼峰命名法工具 * @return * camelCase("hello_world") == "helloWorld" * capCamelCase("hello_world") == "HelloWorld" * uncamelCase("helloWorld") = "hello_world" */ public static String capCamelCase(String s) { if (s == null) { return null; } s = camelCase(s); return s.substring(0, 1).toUpperCase() + s.substring(1); } /** 驼峰命名法工具 * @return * camelCase("hello_world") == "helloWorld" * capCamelCase("hello_world") == "HelloWorld" * uncamelCase("helloWorld") = "hello_world" */ public static String uncamelCase(String s) { if (s == null) { return null; } StringBuilder sb = new StringBuilder(); boolean upperCase = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); boolean nextUpperCase = true; if (i < (s.length() - 1)) { nextUpperCase = Character.isUpperCase(s.charAt(i + 1)); } if ((i > 0) && Character.isUpperCase(c)) { if (!upperCase || !nextUpperCase) { sb.append('_'); } upperCase = true; } else { upperCase = false; } sb.append(Character.toLowerCase(c)); } return sb.toString(); } /** 字符串右补空格 * @see 该方法默认采用空格(其ASCII码为32)来右补字符 */ public static String rightPadForBlank(String str, int size){ return rightPadForByte(str, size, 32); } /** 字符串右补0 * @param str * @param size */ public static String rightPadForZero(String str, int size){ return rightPadForByte(str, size, 48); } /** 字符串右补字符 * @see 若str对应的byte[]长度不小于size,则按照size截取str对应的byte[],而非原样返回str * @see 若对普通字符串进行右补字符,建议org.apache.commons.lang.StringUtils.rightPad(...) * @param size * @param padStrByASCII 该值为所补字符的ASCII码,如 * 32表示空格, * 48表示0, * 64表示@等 */ public static String rightPadForByte(String str, int size, int padStrByASCII){ byte[] srcByte = str.getBytes(); byte[] destByte = null; if(srcByte.length >= size){ //由于size不大于原数组长度,故该方法此时会按照size自动截取,它会在数组右侧填充'(byte)0'以使其具有指定的长度 destByte = Arrays.copyOf(srcByte, size); }else{ destByte = Arrays.copyOf(srcByte, size); Arrays.fill(destByte, srcByte.length, size, (byte)padStrByASCII); } return new String(destByte); } /** 字符串左补空格 * @see 该方法默认采用空格(其ASCII码为32)来左补字符 */ public static String leftPadForBlank(String str, int size){ return leftPadForByte(str, size, 32); } /** 字符串左补0 * @param str * @param size * @return */ public static String leftPadForZero(String str, int size){ return leftPadForByte(str, size, 48); } /** 字符串左补字符 * @see 若str对应的byte[]长度不小于size,则按照size截取str对应的byte[],而非原样返回str * @param padStrByASCII 该值为所补字符的ASCII码,如 * 32表示空格, * 48表示0, * 64表示@等 */ public static String leftPadForByte(String str, int size, int padStrByASCII){ byte[] srcByte = str.getBytes(); byte[] destByte = new byte[size]; Arrays.fill(destByte, (byte)padStrByASCII); if(srcByte.length >= size){ System.arraycopy(srcByte, 0, destByte, 0, size); }else{ System.arraycopy(srcByte, 0, destByte, size-srcByte.length, srcByte.length); } return new String(destByte); } }
============================================================================String格式化工具测试类:
/** * 打印异常信息 */ @Test public void test_getStackTrace() { Exception exception = new Exception("空指针异常"); System.out.println(StringFormatUtil.getStackTrace(exception)); } /** * 驼峰式命名工具 */ @Test public void test_camelCase() { System.out.println(StringFormatUtil.camelCase("hello_world")); System.out.println(StringFormatUtil.capCamelCase("hello_world")); System.out.println(StringFormatUtil.uncamelCase("helloWorld")); } /** 字符串填充 */ @Test public void test_PadForBlank() { String leftPadForBlank = StringFormatUtil.leftPadForBlank("helloWorld", 10); System.out.println(leftPadForBlank); String rigthPadForBlank = StringFormatUtil.rightPadForBlank("helloWorld", 10); System.out.println(rigthPadForBlank); } /** 字符串填充 */ @Test public void test_PadForZero() { String leftPadForZero = StringFormatUtil.leftPadForZero("helloWorld", 10); System.out.println(leftPadForZero); String rigthPadForZero = StringFormatUtil.rightPadForZero("helloWorld", 10); System.out.println(rigthPadForZero); }