StringUtil
来源:互联网 发布:智尚网络 培训 编辑:程序博客网 时间:2024/06/06 04:09
import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import org.apache.commons.lang.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class StringUtil { private static Logger logger = LoggerFactory.getLogger(StringUtil.class); public static String getString(String str) { if(null == str || str.trim().length() == 0) { str = ""; } return str.trim(); } /** * * @Title: isEmpty * @Description: 字符串是否为空 * @param str * @return */ public static boolean isEmpty(String str){ if(null == str || str.trim().length()==0){ return true; } return false; } /** * * @Title: isChinese * @Description: 判断单个字符是否是汉字 * @param mychar * @return */ public static boolean isChinese(char mychar){ int v = (int)mychar; return(v>=19968 && v<=171941); } /** * * @Title: containsChinese * @Description: 判断是否包含汉字 true 是,false 否 * @param s * @return */ public static boolean containsChinese(String s){ if(null == s || "".equals(s.trim())){ return false; } for(int i = 0;i<s.length();i++){ if(isChinese(s.charAt(i)))return true; } return false; } /** * * @Title: isChineseAll * @Description: 判断是否全为汉字,true 是,false 否 * @param s * @return */ public static boolean isChineseAll(String s){ if(null==s||"".equals(s.trim())){ return false; } for(int i= 0;i<s.length();i++){ if(!isChinese(s.charAt(i)))return false; } return true; } /** * 把字符串数组转为以英文逗号分割的字符串 * @param str * @return */ public static String getStringFromArray(String[] str) { String source = ""; for(String s:str){ source += s+","; } source = source.substring(0,source.length()-1); return source; } /** * 把字符串数组转为以英文逗号分割的字符串 * @param str * @return */ public static String getStringFromList(List<Integer> str) { String source = ""; Iterator<Integer> it = str.iterator(); while(it.hasNext()){ source += it.next() + ","; } source = source.substring(0,source.length()-1); return source; } /** * 把字符串数组转为以英文逗号分割的字符串 * @param str * @return */ public static String getStringFromListStr(List<String> str) { String source = ""; Iterator<String> it = str.iterator(); while(it.hasNext()){ source += it.next() + ","; } source = source.substring(0,source.length()-1); return source; } public static String getNameStrFromIdArray(Map<String,String> keyValue,String[] idArray){ String nameString = ""; for(int i = 0 ;i<idArray.length;i++){ nameString += keyValue.get(idArray[i])+","; } nameString = nameString.substring(0,nameString.length()-1); return nameString; } /** * * @Title: isEmail * @Description: 字符串是否是一个 email * @param str * @return */ public static boolean isEmail(String str){ if(StringUtil.isEmpty(str)){ return false; } String reg = "(\\w+)@(\\w+\\.\\w+)"; if(str.matches(reg)){ return true; } return false; } /** * 将格式为:姓名##邮箱&&姓名##邮箱...的字符串拆分为hashmap中 * @param str * @return */ public static LinkedHashMap<String,String> stringToMap(String str){ LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); String[] bb = str.split("&&"); for(int i=0;i<bb.length;i++){ String[] cc = bb[i].split("##"); map.put(cc[0], cc[1]); } return map; } public static String getImpCodeStr(List<String> codes,List<String> subcodes){ StringBuffer codebuf=new StringBuffer(); codes.retainAll(subcodes); if(codes!=null&&codes.size()>0){ codebuf.append("【"); for(String code:codes){ codebuf.append(code+","); } codebuf.deleteCharAt(codebuf.length() - 1); codebuf.append("】"); } return codebuf.toString(); } public static String getUpdCodeStr(List<String> codes,List<String> subcodes){ StringBuffer codebuf=new StringBuffer(); subcodes.retainAll(codes); codes.removeAll(subcodes); if(codes!=null&&codes.size()>0){ codebuf.append("【"); for(String code:codes){ codebuf.append(code+","); } codebuf.deleteCharAt(codebuf.length() - 1); codebuf.append("】"); } return codebuf.toString(); } /** * * @Title: cutStr * @Description: 处理字符串长度,超过指定长度的部分使用"..."代替 * @param @param str 字符串 * @param @param length 指定长度 * @return String 返回类型 * @throws */ public static String cutStr(String str, int length) { if (str == null || "".equals(str)) { return ""; } String resultStr = ""; char[] ch = str.toCharArray(); int count = ch.length; for (int i = 0; i < count; i++) { resultStr += ch[i]; try { if (resultStr.getBytes("UTF-8").length >= length - 3) { resultStr += "..."; break; } } catch (UnsupportedEncodingException e) { //e.printStackTrace(); } } return resultStr; } /** * * @Title: cutStr2 * @Description: 处理字符串长度,超过指定长度的部分不显示 * @param @param str 字符串 * @param @param length 指定长度 * @return String 返回类型 * @throws */ public static String cutStr2(String str, int length) { if (str == null || "".equals(str)) { return ""; } String resultStr = ""; char[] ch = str.toCharArray(); int count = ch.length; for (int i = 0; i < count; i++) { resultStr += ch[i]; try { if (resultStr.getBytes("UTF-8").length >= length) { break; } } catch (UnsupportedEncodingException e) { //e.printStackTrace(); } } return resultStr; } /*** * 处理HTML,将& lt;|<或者& gt;|>转换为<和> * @param writer * @param string * @return * @throws IOException */ public static String unescapeHtml(String string){ String returnString = ""; if(!StringUtils.isEmpty(string)){ string = string.replaceAll("& lt;|<", "<"); returnString = string.replaceAll("& gt;|>", ">"); }else{ string = ""; } return returnString; } public static void main(String[] args){ String aa = "<p& gt;李小龙到底有多牛 武打明星自愧不如</p>"; System.out.println(unescapeHtml(aa)); }
1 0
- StringUtil
- stringutil
- StringUtil
- StringUtil
- stringutil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- StringUtil
- 用Messenger实现Activity与Service的信息交互
- MySQL详解--锁
- 西蒙iphone-OpenGL ES 教程-07 : 对物体进行独立的转换
- opencv contours 遇到边界
- 在Windows环境下学习Linux命令行的几种方法
- StringUtil
- JavaScript应该被放在什么位置
- 无法解析的外部符号 _main解决办法
- [libevent]event_add()
- 配置gulpfile文件,实现文件更改后浏览器即时刷新
- <JAVA与模式>之动态代理
- rapidxml 写数字节点的问题
- 源码-PL/SQL从入门到精通-第十三章-子程序-Part 1
- GreenDao的简单使用说明(六)补遗