java 常用方法:
(1)去掉所有的回车换行
class="java">/** * delete CRLF; delete empty line ;delete blank lines * * @param input * @return */ public static String deleteCRLF(String input) { input = SystemHWUtil.deleteCRLFOnce(input); return SystemHWUtil.deleteCRLFOnce(input); } /*** * delete CRLF; delete empty line ;delete blank lines * * @param input * @return */ private static String deleteCRLFOnce(String input) { if (ValueWidget.isHasValue(input)) { return input.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1") .replaceAll("^((\r\n)|\n)", ""); } else { return null; } }
?使用时直接调用deleteCRLF方法即可
(2)去掉所有的空格
/*** * Delete all spaces * * @param input * @return */ public static String deleteAllCRLF(String input) { return input.replaceAll("((\r\n)|\n)[\\s\t ]*", "").replaceAll( "^((\r\n)|\n)", ""); }
?
(3)去掉String[]中重复的元素,去重
/*** * test ok!<br> 去重 * @param strs * @return */ public static String[] guolv(String[]strs){ List<String>list =new ArrayList<String>(); list.add(strs[0]);//数组的第一个元素 for(int i=1;i<strs.length;i++){ String string=strs[i]; if(!isContains(list, string)){ list.add(string); } } return list2Arr(list); } /*** * 判断list中是否包含keyWord * * @param list * @param keyWord * @return */ public static boolean isContains(List<String> list, String keyWord) { if (ValueWidget.isNullOrEmpty(list)) { return false; } return list.contains(keyWord); } /*** * convert List to String[] * @param list * @return */ public static String[] list2Arr(List list){ int size=list.size(); String[]strs=new String[size]; for(int i=0;i<size;i++){ strs[i]=(String)list.get(i); } return strs; }
?
(4)把List 转化为String[]
/*** * convert List to String[] * @param list * @return */ public static String[] list2Arr(List list){ int size=list.size(); String[]strs=new String[size]; for(int i=0;i<size;i++){ strs[i]=(String)list.get(i); } return strs; }
?(5)获取目录中的文件名
/*** * 获取文件名称。例如"aa/bb我们#.txt"-->“bb我们#.txt” * * @param filepath * @return */ public static String getFileNameOnly(String filepath) { String result = filepath.replaceAll(".*\\b[/\\\\]([\\w\\.]+)", "$1"); return result; }
?注意:目录的类型是字符串
(6)过滤掉List中相同的元素
/*** * 过滤掉其中相同的元素,test ok * * @param list * @param propertyName * @param propertyValue * @return * @throws SecurityException * @throws IllegalArgumentException * @throws NoSuchFieldException * @throws IllegalAccessException */ public static List<?> uniqueObject(List<?> list, String propertyName) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { if (ValueWidget.isNullOrEmpty(list)) { return list; } List resultList = new ArrayList(); resultList.add(list.get(0)); for (int i = 1; i < list.size(); i++) { Object obj = list.get(i); if (!isContainObject(resultList, propertyName, (String) ReflectHWUtils.getObjectValue(obj, propertyName))) { resultList.add(obj); } } return resultList; } /*** * 判断 list 中是否已经存在该对象 * * @param list * @param propertyName * @param propertyValue * @return * @throws SecurityException * @throws IllegalArgumentException * @throws NoSuchFieldException * @throws IllegalAccessException */ public static boolean isContainObject(List list, String propertyName, String propertyValue) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { if (ValueWidget.isNullOrEmpty(list)) { return false; } for (int i = 0; i < list.size(); i++) { Object obj = list.get(i); String propertyValue2 = (String) ReflectHWUtils.getObjectValue(obj, propertyName); if (propertyValue.equals(propertyValue2)) { return true; } } return false; } /*** * 获取指定对象的属性值 * * @param obj * @param propertyName * @return * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Object getObjectValue(Object obj, String propertyName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { if (StringUtils.isEmpty(propertyName)) { return null; } Class<?> clazz = obj.getClass(); Field name = getSpecifiedField(clazz, propertyName); if (ValueWidget.isNullOrEmpty(name)) { propertyName=ValueWidget.title(propertyName);//convert "Key2" to "key2" name = getSpecifiedField(clazz, propertyName); if (ValueWidget.isNullOrEmpty(name)) { System.out.println("[ReflectHWUtils.getObjectValue]" + obj.getClass().getName() + " does not has field " + propertyName); return null; } } return getObjectValue(obj, name); }
?注意:List中的元素不是基本类型