String 工具类
2018-10-03 17:57:44来源:博客园 阅读 ()
package com.mytripod.util; import sun.rmi.runtime.Log; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.util.List; import java.util.StringTokenizer; /** * @author Mytripod * @create 2018-09-30 21:30 */ public class StringUtil { private static Log log = LogFactory.getLog(StringUtil.class); public static String ENCODING = "UTF-8"; public static String encodePassword(String password, String algorithm) { byte[] unencodedPassword = password.getBytes(); MessageDigest md = null; try { md = MessageDigest.getInstance(algorithm); } catch (Exception e) { log.error("Exception: " + e); return password; } md.reset(); md.update(unencodedPassword); byte[] encodedPassword = md.digest(); StringBuffer buf = new StringBuffer(); for (int i = 0; i < encodedPassword.length; i++) { if (((int) encodedPassword[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString((int) encodedPassword[i] & 0xff, 16)); } return buf.toString(); } public static String encodeString(String str) { if (str == null) return null; try { byte[] b = Base64.encodeBase64(str.getBytes(ENCODING)); return new String(b, ENCODING); } catch (UnsupportedEncodingException e) { return null; } } public static String encodeStringURLSafe(String str) { if (str == null) return null; try { byte[] b = Base64.encodeBase64URLSafe(str.getBytes(ENCODING)); return new String(b, ENCODING); } catch (UnsupportedEncodingException e) { return null; } } public static String decodeString(String str) { if (str == null) return null; try { byte[] b = Base64.decodeBase64(str.getBytes(ENCODING)); return new String(b, ENCODING); } catch (Exception e) { return null; } } public static String swapFirstLetterCase(String string) { StringBuffer sbuf = new StringBuffer(string); sbuf.deleteCharAt(0); if (Character.isLowerCase(string.substring(0, 1).toCharArray()[0])) { sbuf.insert(0, string.substring(0, 1).toUpperCase()); } else { sbuf.insert(0, string.substring(0, 1).toLowerCase()); } return sbuf.toString(); } public static String trim(String origString, String trimString) { int startPosit = origString.indexOf(trimString); if (startPosit != -1) { int endPosit = trimString.length() + startPosit; return origString.substring(0, startPosit) + origString.substring(endPosit); } return origString; } public static boolean contain(String origString, String containString) { if (origString.indexOf(containString) != -1) { return true; } return false; } /** * * @param origString * @param stringToken */ public static String getLastString(String origString, String stringToken) { StringTokenizer st = new StringTokenizer(origString, stringToken); String lastString = ""; while (st.hasMoreTokens()) { lastString = st.nextToken(); } return lastString; } /** * @param string * @param token */ public static String[] getStringArray(String string, String token) { if (string.indexOf(token) != -1) { StringTokenizer st = new StringTokenizer(string, token); String[] stringArray = new String[st.countTokens()]; for (int i = 0 ; st.hasMoreTokens(); i++) { stringArray[i] = st.nextToken(); } return stringArray; } return new String [] {string}; } public static String[] getStringArray(String string) { StringTokenizer st = new StringTokenizer(string); String[] stringArray = new String[st.countTokens()]; for (int i = 0 ; st.hasMoreTokens(); i++) { stringArray[i] = st.nextToken(); } return stringArray; } public static String replace(String string, String replaced, String replace) { String newString = ""; if (string.indexOf(replaced) != -1 ) { String s1 = string.substring(0, string.indexOf(replaced)); String s2 = string.substring(string.indexOf(replaced) + 1);; newString = s1 + replace + s2; } return newString; } public static boolean hasLength(String str) { return (str != null && str.length() > 0); } public static boolean hasText(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return false; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(str.charAt(i))) { return true; } } return false; } public static String toString(Object obj) { return ToStringBuilder.reflectionToString(obj,ToStringStyle.DEFAULT_STYLE); } public static void main(String[] args) { String s = "work$id"; s = StringUtil.replace(s, "$", "."); System.out.print(s); } public static boolean notEmpty(String str) { if ( str == null ) return false; else if ( str.equals("") ) return false; else return true; } public static boolean isNotEmpty(String s) { return s != null && !s.trim().equals(""); } public static boolean isEmpty(String s) { return s == null || s.trim().equals(""); } public static boolean isNotNull(String s) { return s != null; } public static boolean isEqual(String s, Object o) { return isNotEmpty(s) && s.equals(o); } /** * 将字符串数组转化为in条件用的字符串 * @param str * @return */ public static String getInClause(String[] str) { StringBuffer result = new StringBuffer(); for(int i=0; i<str.length; i++) { if(i>0) { result.append(","); } result.append("'" + str[i] +"'"); } return result.toString(); } public static String getInClause(List<String> list) { String[] arr = (String[])list.toArray(new String[list.size()]); return getInClause(arr); } public static boolean isNumeric(String str){ for(int i=str.length();--i>=0;){ int chr=str.charAt(i); if(chr<48 || chr>57) return false; } return true; } public static String jsonString(String s){ String str = ""; char[] temp = s.toCharArray(); int n = temp.length; for(int i =0;i<n;i++){ if(temp[i]=='"'){ str = str + "”"; }else{ str = str + temp[i]; } } return str; } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:Java SQL注入学习笔记
- 代码对比工具,我就用这 6 个! 2020-06-10
- springboot~注册不同级别包里的bean~scanBasePackages 2020-06-10
- 2020最新IDEA插件大集合,一款能帮助你写代码的工具是多么重 2020-06-09
- 「starter推荐」简单高效Excel 导出工具 2020-06-08
- java里面main方法中的String[]args 2020-06-07
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash