String 工具类

2018-10-03 17:57:44来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用dom4j写xml文件——源码

下一篇:Java SQL注入学习笔记