一聚教程网:一个值得你收藏的教程网站

热门教程

Java实现的文本字符串操作工具类实例【数据替换,加密解密操作】

时间:2022-06-29 01:07:47 编辑:袖梨 来源:一聚教程网

本文实例讲述了Java实现的文本字符串操作工具类。分享给大家供大家参考,具体如下:

package com.gcloud.common;
import org.apache.commons.lang.StringUtils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
/**
 * 文本字符串工具类
 * Created by charlin on 2017/9/10.
 */
public class TextUtil {
  private static final char[] QUOTE_ENCODE = """.toCharArray();
  private static final char[] AMP_ENCODE = "&".toCharArray();
  private static final char[] LT_ENCODE = "".toCharArray();
  private final static String[] hex = { "00", "01", "02", "03", "04", "05",
      "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10",
      "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B",
      "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26",
      "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31",
      "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C",
      "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47",
      "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52",
      "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D",
      "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68",
      "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73",
      "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E",
      "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
      "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94",
      "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
      "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA",
      "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5",
      "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0",
      "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB",
      "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6",
      "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1",
      "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC",
      "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7",
      "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" };
  private final static byte[] val = { 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x01,
      0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F };
  private static MessageDigest digest = null;
  /**
   * 替换字符串
   * @param line
   * @param oldStr
   * @param newStr
   * @return
   */
  public static String replace(String line , String oldStr, String newStr){
    if (StringUtils.isEmpty(line)) return null;
    int index = 0;
    if ((index = line.indexOf(oldStr, index)) >= 0){
      char[] charArray = line.toCharArray();
      char[] newStrArray = newStr.toCharArray();
      int oldLen = oldStr.length();
      StringBuffer buf = new StringBuffer(charArray.length);
      buf.append(charArray, 0, index).append(newStrArray);
      index += oldLen;
      int i = index;
      while((index = line.indexOf(oldStr, index)) > 0){
        buf.append(charArray, i, index - i).append(newStrArray);
        index += oldLen;
        i = index;
      }
      buf.append(charArray, i, charArray.length - i);
      return buf.toString();
    }
    return line;
  }
  /**
   * 替换字符串,并存入替换次数
   * @param line
   * @param oldStr
   * @param newStr
   * @return
   */
  public static String replace(String line , String oldStr, String newStr, int[] countArr){
    if (StringUtils.isEmpty(line)) return null;
    int index = 0;
    if ((index = line.indexOf(oldStr, index)) >= 0){
      int count = 0;
      count++;
      char[] charArray = line.toCharArray();
      char[] newStrArray = newStr.toCharArray();
      int oldLen = oldStr.length();
      StringBuffer buf = new StringBuffer(charArray.length);
      buf.append(charArray, 0, index).append(newStrArray);
      index += oldLen;
      int i = index;
      while((index = line.indexOf(oldStr, index)) > 0){
        buf.append(charArray, i, index - i).append(newStrArray);
        index += oldLen;
        i = index;
        count++;
      }
      buf.append(charArray, i, charArray.length - i);
      countArr[0] = count;
      return buf.toString();
    }
    return line;
  }
  /**
   * 替换字符串,不区分大小写
   * @param line
   * @param oldStr
   * @param newStr
   * @return
   */
  public static String replaceIgnoreCase(String line , String oldStr, String newStr){
    if (StringUtils.isEmpty(line)) return null;
    int index = 0;
    line = line.toLowerCase();
    oldStr = oldStr.toLowerCase();
    if ((index = line.indexOf(oldStr, index)) >= 0){
      char[] charArray = line.toCharArray();
      char[] newStrArray = newStr.toCharArray();
      int oldLen = oldStr.length();
      StringBuffer buf = new StringBuffer(charArray.length);
      buf.append(charArray, 0, index).append(newStrArray);
      index += oldLen;
      int i = index;
      while((index = line.indexOf(oldStr, index)) > 0){
        buf.append(charArray, i, index - i).append(newStrArray);
        index += oldLen;
        i = index;
      }
      buf.append(charArray, i, charArray.length - i);
      return buf.toString();
    }
    return line;
  }
  /**
   * 替换字符串,不区分大小写, 并存入替换次数
   * @param line
   * @param oldStr
   * @param newStr
   * @return
   */
  public static String replaceIgnoreCase(String line , String oldStr, String newStr, int[] countArr){
    if (StringUtils.isEmpty(line)) return null;
    int index = 0;
    line = line.toLowerCase();
    oldStr = oldStr.toLowerCase();
    if ((index = line.indexOf(oldStr, index)) >= 0){
      char[] charArray = line.toCharArray();
      char[] newStrArray = newStr.toCharArray();
      int oldLen = oldStr.length();
      StringBuffer buf = new StringBuffer(charArray.length);
      buf.append(charArray, 0, index).append(newStrArray);
      index += oldLen;
      int i = index;
      int count = 0;
      while((index = line.indexOf(oldStr, index)) > 0){
        count ++;
        buf.append(charArray, i, index - i).append(newStrArray);
        index += oldLen;
        i = index;
      }
      buf.append(charArray, i, charArray.length - i);
      countArr[0] = count;
      return buf.toString();
    }
    return line;
  }
  /**
   * 替换页面html标签
   * @param htmlStr
   * @return
   */
  public static String escapeHTMLTags(String htmlStr) {
    if (StringUtils.isEmpty(htmlStr)) return null;
    char ch;
    int last = 0;
    char[] htmlStrArr = htmlStr.toCharArray();
    int len = htmlStrArr.length;
    StringBuffer outBuf = new StringBuffer((int) (len * 1.3));
    int i = 0;
    for (; i  '>') {
        continue;
      } else if (ch == ' last) {
          outBuf.append(htmlStrArr, last, i - last);
        }
        last = i + 1;
        outBuf.append(LT_ENCODE);
      } else if (ch == '>') {
        if (i > last) {
          outBuf.append(htmlStrArr, last, i - last);
        }
        last = i + 1;
        outBuf.append(GT_ENCODE);
      }
    }
    if (last == 0) {
      return htmlStr;
    }
    if (i > last) {
      outBuf.append(htmlStrArr, last, i - last);
    }
    return outBuf.toString();
  }
  /**
   * Hashes a String using the Md5 algorithm and returns the result as a
   * String of hexadecimal numbers. This method is synchronized to avoid
   * excessive MessageDigest object creation. If calling this method becomes a
   * bottleneck in your code, you may wish to maintain a pool of MessageDigest
   * objects instead of using this method.
   * @param data
   * @return
   */
  public synchronized static String hash(String data) {
    if (digest == null) {
      try {
        digest = MessageDigest.getInstance("MD5");
      } catch (NoSuchAlgorithmException e) {
        System.err.println("Failed to load the MD5 MessageDigest.Jive will be unable to function normally.");
        e.printStackTrace();
      }
    }
    digest.update(data.getBytes());
    return encodeHex(digest.digest());
  }
  /**
   * Turns an array of bytes into a String representing each byte as an
   * unsigned hex number.
   * @param bytes
   * @return
   */
  public static final String encodeHex(byte[] bytes) {
    StringBuffer buf = new StringBuffer(bytes.length * 2);
    int i;
    for (i = 0; i > 2) & 0x3f;
      ret.append(cvt.charAt(c));
      c = (data[i] > 4) & 0x0f;
      ret.append(cvt.charAt(c));
      if (i > 6) & 0x03;
        ret.append(cvt.charAt(c));
      } else {
        ++i;
        ret.append((char) fillchar);
      }
      if (i > 4) & 0x3));
      ret.append((char) c);
      if (++i > 2) & 0xf);
        ret.append((char) c1);
      }
      if (++i WordArray(String text) {
    if (text == null || text.length() == 0) {
      return new String[0];
    }
    ArrayList wordList = new ArrayList();
    BreakIterator boundary = BreakIterator.getWordInstance();
    boundary.setText(text);
    int start = 0;
    for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
      String tmp = text.substring(start, end).trim();
      tmp = replace(tmp, "+", "");
      tmp = replace(tmp, "/", "");
      tmp = replace(tmp, "", "");
      tmp = replace(tmp, "#", "");
      tmp = replace(tmp, "*", "");
      tmp = replace(tmp, ")", "");
      tmp = replace(tmp, "(", "");
      tmp = replace(tmp, "&", "");
      if (tmp.length() > 0) {
        wordList.add(tmp);
      }
    }
    return (String[]) wordList.toArray(new String[wordList.size()]);
  }
  /**
   * Pseudo-random number generator object for use with randomString(). The
   * Random class is not considered to be cryptographically secure, so only
   * use these random Strings for low to medium security applications.
   */
  private static Random randGen = new Random();
  /**
   * Array of numbers and letters of mixed case. Numbers appear in the list
   * twice so that there is a more equal chance that a number will be picked.
   * We can use the array to get a random number or letter by picking a random
   * array index.
   */
  private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
  /**
   * Returns a random String of numbers and letters (lower and upper case) of
   * the specified length. The method uses the Random class that is built-in
   * to Java which is suitable for low to medium grade security uses. This
   * means that the output is only pseudo random, i.e., each number is
   * mathematically generated so is not truly random.
   * @param length the desired length of the random String to return.
   * @return a random String of numbers and letters of the specified length.
   */
  public static final String randomString(int length) {
    if (length length, the String will be
   * chopped there. If no newline or whitespace is found in
   * string up to the index length, the String
   * will chopped at length.
   * 

* For example, chopAtWord("This is a nice String", 10) will return "This is * a" which is the first word boundary less than or equal to 10 characters * into the original String. * @param string the String to chop. * @param length * @return a substring of string whose length is less than or * equal to length, and that is chopped at * whitespace. */ public static final String chopAtWord(String string, int length) { if (string == null) { return string; } char[] charArray = string.toCharArray(); int sLength = string.length(); if (length 0; i--) { if (charArray[i] == ' ') { return string.substring(0, i).trim(); } } // Did not find word boundary so return original String chopped at // specified length. return string.substring(0, length); } /** * Escapes all necessary characters in the String so that it can be used in * an XML doc. * @param string the string to escape. * @return the string with appropriate characters escaped. */ public static final String escapeForXML(String string) { if (string == null) { return null; } char ch; int i = 0; int last = 0; char[] input = string.toCharArray(); int len = input.length; StringBuffer out = new StringBuffer((int) (len * 1.3)); for (; i '>') { continue; } else if (ch == ' last) { out.append(input, last, i - last); } last = i + 1; out.append(LT_ENCODE); } else if (ch == '&') { if (i > last) { out.append(input, last, i - last); } last = i + 1; out.append(AMP_ENCODE); } else if (ch == '"') { if (i > last) { out.append(input, last, i - last); } last = i + 1; out.append(QUOTE_ENCODE); } } if (last == 0) { return string; } if (i > last) { out.append(input, last, i - last); } return out.toString(); } /** * Unescapes the String by converting XML escape sequences back into normal * characters. * @param string the string to unescape. * @return the string with appropriate characters unescaped. */ public static final String unescapeFromXML(String string) { string = replace(string, "", ">"); string = replace(string, """, """); return replace(string, "&", "&"); } public static String escape(String s) { StringBuffer sbuf = new StringBuffer(); int len = s.length(); for (int i = 0; i >> 8)]); sbuf.append(hex[(0x00FF & ch)]); } } return sbuf.toString(); } public static String unescape(String s) { StringBuffer sbuf = new StringBuffer(); int i = 0; int len = s.length(); while (i length of 16. * * @param string * the original String to pad. * @param length * the desired length of the new padded String. * @return a new String padded with the required number of 0's. */ public static final String zeroPadString(String string, int length) { if (string == null || string.length() > length) { return string; } StringBuffer buf = new StringBuffer(length); buf.append(zeroArray, 0, length - string.length()).append(string); return buf.toString(); } /** * Formats a Date as a fifteen character long String made up of the Date's * padded millisecond value. * @return a Date encoded as a String. */ public static final String dateToMillis(Date date) { return zeroPadString(Long.toString(date.getTime()), 15); } public static void main(String[] args) { System.out.println(replace("aaaaabbbcccc", "aa", "gg")); System.out.println(replaceIgnoreCase("AAAAbbbcccc", "aa", "gg")); System.out.println(escapeHTMLTags("AAAAbb")); } }

热门栏目