Java实现的文本字符串操作工具类实例【数据替换,加密解密操作】
程序员文章站
2024-04-02 09:34:10
本文实例讲述了java实现的文本字符串操作工具类。分享给大家供大家参考,具体如下:
package com.gcloud.common;
import org....
本文实例讲述了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 static final char[] gt_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 < len; i++) { ch = htmlstrarr[i]; if (ch > '>') { continue; } else if (ch == '<') { if (i > 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 < bytes.length; i++) { if (((int) bytes[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(long.tostring((int) bytes[i] & 0xff, 16)); } return buf.tostring(); } /** * turns a hex encoded string into a byte array. it is specifically meant to * "reverse" the tohex(byte[]) method. * @param hex * @return */ public static byte[] decodehex(string hex) { char[] chars = hex.tochararray(); byte[] bytes = new byte[chars.length / 2]; int bytecount = 0; for (int i = 0; i < chars.length; i += 2) { byte newbyte = 0x00; newbyte |= hexchartobyte(chars[i]); newbyte <<= 4; newbyte |= hexchartobyte(chars[i + 1]); bytes[bytecount] = newbyte; bytecount++; } return bytes; } /** * returns the the byte value of a hexadecmical char (0-f). it's assumed * that the hexidecimal chars are lower case as appropriate. * @param ch a hexedicmal character (0-f) * @return the byte value of the character (0x00-0x0f) */ private static final byte hexchartobyte(char ch) { switch (ch) { case '0': return 0x00; case '1': return 0x01; case '2': return 0x02; case '3': return 0x03; case '4': return 0x04; case '5': return 0x05; case '6': return 0x06; case '7': return 0x07; case '8': return 0x08; case '9': return 0x09; case 'a': return 0x0a; case 'b': return 0x0b; case 'c': return 0x0c; case 'd': return 0x0d; case 'e': return 0x0e; case 'f': return 0x0f; } return 0x00; } // ********************************************************************* // * base64 - a simple base64 encoder and decoder. // ********************************************************************* /** * encodes a string as a base64 string. * @param data a string to encode. * @return a base64 encoded string. */ public static string encodebase64(string data) { return encodebase64(data.getbytes()); } /** * encodes a byte array into a base64 string. * @param data a byte array to encode. * @return a base64 encode string. */ public static string encodebase64(byte[] data) { int c; int len = data.length; stringbuffer ret = new stringbuffer(((len / 3) + 1) * 4); for (int i = 0; i < len; ++i) { c = (data[i] >> 2) & 0x3f; ret.append(cvt.charat(c)); c = (data[i] << 4) & 0x3f; if (++i < len) c |= (data[i] >> 4) & 0x0f; ret.append(cvt.charat(c)); if (i < len) { c = (data[i] << 2) & 0x3f; if (++i < len) c |= (data[i] >> 6) & 0x03; ret.append(cvt.charat(c)); } else { ++i; ret.append((char) fillchar); } if (i < len) { c = data[i] & 0x3f; ret.append(cvt.charat(c)); } else { ret.append((char) fillchar); } } return ret.tostring(); } /** * decodes a base64 string. * @param data a base64 encoded string to decode. * @return the decoded string. */ public static string decodebase64(string data) { return decodebase64(data.getbytes()); } /** * decodes a base64 aray of bytes. * @param data a base64 encode byte array to decode. * @return the decoded string. */ public static string decodebase64(byte[] data) { int c, c1; int len = data.length; stringbuffer ret = new stringbuffer((len * 3) / 4); for (int i = 0; i < len; ++i) { c = cvt.indexof(data[i]); ++i; c1 = cvt.indexof(data[i]); c = ((c << 2) | ((c1 >> 4) & 0x3)); ret.append((char) c); if (++i < len) { c = data[i]; if (fillchar == c) break; c = cvt.indexof((char) c); c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); ret.append((char) c1); } if (++i < len) { c1 = data[i]; if (fillchar == c1) break; c1 = cvt.indexof((char) c1); c = ((c << 6) & 0xc0) | c1; ret.append((char) c); } } return ret.tostring(); } private static final int fillchar = '='; private static final string cvt = "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; /** * converts a line of text into an array of lower case words using a * breakiterator.wordinstance(). * this method is under the jive open source software license and was * written by mark imbriaco. * @param text a string of text to convert into an array of words * @return text broken up into an array of words. */ public static final string[] tolowercasewordarray(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 < 1) { return null; } char[] randbuffer = new char[length]; for (int i = 0; i < randbuffer.length; i++) { randbuffer[i] = numbersandletters[randgen.nextint(71)]; } return new string(randbuffer); } /** * intelligently chops a string at a word boundary (whitespace) that occurs * at the specified index in the argument or before. however, if there is a * newline character before <code>length</code>, the string will be * chopped there. if no newline or whitespace is found in * <code>string</code> up to the index <code>length</code>, the string * will chopped at <code>length</code>. * <p> * 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 <code>string</code> whose length is less than or * equal to <code>length</code>, 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 < slength) { slength = length; } // first check if there is a newline character before length; if so, // chop word there. for (int i = 0; i < slength - 1; i++) { // windows if (chararray[i] == '\r' && chararray[i + 1] == '\n') { return string.substring(0, i + 1); } // unix else if (chararray[i] == '\n') { return string.substring(0, i); } } // also check boundary case of unix newline if (chararray[slength - 1] == '\n') { return string.substring(0, slength - 1); } // done checking for newline, now see if the total string is less than // the specified chop point. if (string.length() < length) { return string; } // no newline, so chop at the first whitespace. for (int i = length - 1; i > 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 < len; i++) { ch = input[i]; if (ch > '>') { continue; } else if (ch == '<') { if (i > 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, ">", ">"); 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 < len; i++) { int ch = s.charat(i); if (ch == ' ') { // space : map to '+' sbuf.append('+'); } else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was sbuf.append((char) ch); } else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was sbuf.append((char) ch); } else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was sbuf.append((char) ch); } else if (ch == '-' || ch == '_' // unreserved : as it was || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' || ch == '(' || ch == ')') { sbuf.append((char) ch); } else if (ch <= 0x007f) { // other ascii : map to %xx sbuf.append('%'); sbuf.append(hex[ch]); } else { // unicode : map to %uxxxx sbuf.append('%'); sbuf.append('u'); sbuf.append(hex[(ch >>> 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 < len) { int ch = s.charat(i); if (ch == '+') { // + : map to ' ' sbuf.append(' '); } else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was sbuf.append((char) ch); } else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was sbuf.append((char) ch); } else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was sbuf.append((char) ch); } else if (ch == '-' || ch == '_' // unreserved : as it was || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' || ch == '(' || ch == ')') { sbuf.append((char) ch); } else if (ch == '%') { int cint = 0; if ('u' != s.charat(i + 1)) { // %xx : map to ascii(xx) cint = (cint << 4) | val[s.charat(i + 1)]; cint = (cint << 4) | val[s.charat(i + 2)]; i += 2; } else { // %uxxxx : map to unicode(xxxx) cint = (cint << 4) | val[s.charat(i + 2)]; cint = (cint << 4) | val[s.charat(i + 3)]; cint = (cint << 4) | val[s.charat(i + 4)]; cint = (cint << 4) | val[s.charat(i + 5)]; i += 5; } sbuf.append((char) cint); } i++; } return sbuf.tostring(); } private static final char[] zeroarray = "0000000000000000".tochararray(); /** * pads the supplied string with 0's to the specified length and returns the * result as a new string. for example, if the initial string is "9999" and * the desired length is 8, the result would be "00009999". this type of * padding is useful for creating numerical values that need to be stored * and sorted as character data. note: the current implementation of this * method allows for a maximum <tt>length</tt> 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<bcccc>")); } }
更多关于java算法相关内容感兴趣的读者可查看本站专题:《java字符与字符串操作技巧总结》、《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。