String工具类(持续更新)
1.获取object中某一个参数值
/** * @param obj * @param fildName * @return */ public static List<String> objToString(List<?> obj,String fildName) { List<String> strings = new ArrayList<>(); try{ for (Object o : obj) { Class<?> Clazz = o.getClass(); Field field = Clazz.getDeclaredField(fildName); field.setAccessible(true); Object fieValue = field.get(o); strings.add(fieValue.toString()); field.setAccessible(false); } return strings; }catch (Exception e){ e.printStackTrace(); } return null; }
2.基础类型转换
// String conver Char
public static Character stringToChar(String str){ if(StringUtil.isNotEmpty(str)){ char[] chars = str.toCharArray(); return chars[0]; } return 0; } // String conver Integer public static Integer stringToInteger(String str){ if (StringUtil.isNotEmpty(str)){ return Integer.valueOf(str); } return null; } // Map conver Object public static <T>T mapToObj(Map<String,Object> map, Class<T> tClass) { return JSONObject.parseObject(JSONObject.toJSONString(map), (Type) tClass); } // Object conver Map public static Map<String, Object> objToMap(Object obj) { String jsonStr = JSONObject.toJSONString(obj); return JSONObject.parseObject(jsonStr); }
本文来自博客园,作者:小辉辉。。,转载请注明原文链接:https://www.cnblogs.com/zjylsh/p/15507328.html