Java tutorial
/* * Apache License, Version 2.0??????? * ????????????? * * ??http://www.apache.org/licenses/LICENSE-2.0????? * * ??????????????? * ???????? * ?????????????????????? * * ???????????????????????? */ package jp.co.golorp.emarf.util; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; /** * ? * * @author oukuf@golorp */ public final class StringUtil extends StringUtils { /** * */ private StringUtil() { } /** * ?? * * @param s * * @return */ public static String toCamelCase(final String s) { if (s == null) { return null; } StringBuilder sb = new StringBuilder(); // "_"? String[] pieces = s.split("_"); // "_"???????? // UpperCamelCase ??? // ?????? if (pieces.length == 1 && !s.matches("^[0-9A-Z]+$")) { pieces = s.split("(?=[A-Z])"); } for (String piece : pieces) { if (sb.toString().equals("")) { // ???? sb.append(piece.toLowerCase()); } else { // ??????? sb.append(piece.substring(0, 1).toUpperCase()); sb.append(piece.substring(1).toLowerCase()); } } return sb.toString(); } /** * ?? * * @param s * * @return */ public static String toUpperCamelCase(final String s) { if (StringUtil.isBlank(s)) { return null; } String camelCase = StringUtil.toCamelCase(s); return camelCase.substring(0, 1).toUpperCase() + camelCase.substring(1); } /** * @param o * o * @return boolean */ public static boolean isBlank(final Object o) { return !isNotBlank(o); } /** * @param o * o * @return boolean */ public static boolean isNotBlank(final Object o) { if (o != null) { if (o instanceof String[]) { String[] strings = (String[]) o; for (String string : strings) { CharSequence cs = string; if (StringUtil.isNotBlank(cs)) { return true; } } } else { String s = String.valueOf(o); if (StringUtil.isNotBlank(s)) { return true; } } } return false; } /** * @param s * * @return true?? */ public static boolean is(final String s) { if (s != null && s.equals("true")) { return true; } return false; } /** * @param value * value * @return String */ public static String getValue(final Object value) { if (value instanceof String[]) { String s = ""; for (String v : (String[]) value) { if (!s.equals("")) { s += ", "; } s += v; } return s; } if (value != null) { return value.toString(); } return null; } /** * @param value * * @return ? */ public static String[] toStringArray(final Object value) { String[] values = null; if (value instanceof String[]) { values = (String[]) value; } else if (value instanceof Object[]) { List<String> valueList = new ArrayList<String>(); for (Object o : (Object[]) value) { valueList.add(String.valueOf(o)); } values = valueList.toArray(new String[valueList.size() - 1]); } else if (value instanceof List) { List<?> valueList = (List<?>) value; if (valueList.size() > 0) { values = valueList.toArray(new String[valueList.size() - 1]); } } else if (value != null) { values = new String[] { String.valueOf(value) }; } return values; } /** * @param stringArray * stringArray * @return ????? */ public static String getFirst(final String[] stringArray) { String s = null; for (int i = 0; i < stringArray.length; i++) { s = stringArray[i]; if (StringUtil.isNotBlank(s)) { break; } } return s; } /** * @param stringArray * stringArray * @return ???? */ public static String getLast(final String[] stringArray) { String s = null; for (int i = stringArray.length - 1; i > 0; i--) { s = stringArray[i]; if (StringUtil.isNotBlank(s)) { break; } } return s; } /** * @param s * s * @param str * str * @return int */ public static int indexOfIgnoreCase(final String s, final String str) { String[] strs = s.split("(?i)" + str); if (s.length() == strs[0].length()) { return -1; } return strs[0].length(); } /** * @param s * s * @return \b,\r,\n," "?? */ public static String trim(final String s) { String ret = StringUtils.trim(s); if (ret != null) { ret.replaceAll("\\b|\\r|\\n", "").trim(); } return ret; } /** * @param s * s * @return ,???? */ public static String[] split(final String s) { if (isNotBlank(s)) { return s.split(",\\s*"); } return null; } }