⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutils.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/** * $Revision: 5718 $ * $Date: 2006-10-13 17:18:18 -0700 (Fri, 13 Oct 2006) $ * * Copyright (C) 2004-2006 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Public License (GPL), * a copy of which is included in this distribution. */package org.jivesoftware.util;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.text.BreakIterator;import java.util.*;import java.util.concurrent.ConcurrentHashMap;/** * Utility class to peform common String manipulation algorithms. */public class StringUtils {    // Constants used by escapeHTMLTags    private static final char[] QUOTE_ENCODE = "&quot;".toCharArray();    private static final char[] AMP_ENCODE = "&amp;".toCharArray();    private static final char[] LT_ENCODE = "&lt;".toCharArray();    private static final char[] GT_ENCODE = "&gt;".toCharArray();    private StringUtils() {        // Not instantiable.    }    /**     * Replaces all instances of oldString with newString in string.     *     * @param string the String to search to perform replacements on.     * @param oldString the String that should be replaced by newString.     * @param newString the String that will replace all instances of oldString.     * @return a String will all instances of oldString replaced by newString.     */    public static String replace(String string, String oldString, String newString) {        if (string == null) {            return null;        }        int i = 0;        // Make sure that oldString appears at least once before doing any processing.        if ((i = string.indexOf(oldString, i)) >= 0) {            // Use char []'s, as they are more efficient to deal with.            char[] string2 = string.toCharArray();            char[] newString2 = newString.toCharArray();            int oLength = oldString.length();            StringBuilder buf = new StringBuilder(string2.length);            buf.append(string2, 0, i).append(newString2);            i += oLength;            int j = i;            // Replace all remaining instances of oldString with newString.            while ((i = string.indexOf(oldString, i)) > 0) {                buf.append(string2, j, i - j).append(newString2);                i += oLength;                j = i;            }            buf.append(string2, j, string2.length - j);            return buf.toString();        }        return string;    }    /**     * Replaces all instances of oldString with newString in line with the     * added feature that matches of newString in oldString ignore case.     *     * @param line      the String to search to perform replacements on     * @param oldString the String that should be replaced by newString     * @param newString the String that will replace all instances of oldString     * @return a String will all instances of oldString replaced by newString     */    public static String replaceIgnoreCase(String line, String oldString,                                                 String newString) {        if (line == null) {            return null;        }        String lcLine = line.toLowerCase();        String lcOldString = oldString.toLowerCase();        int i = 0;        if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {            char[] line2 = line.toCharArray();            char[] newString2 = newString.toCharArray();            int oLength = oldString.length();            StringBuilder buf = new StringBuilder(line2.length);            buf.append(line2, 0, i).append(newString2);            i += oLength;            int j = i;            while ((i = lcLine.indexOf(lcOldString, i)) > 0) {                buf.append(line2, j, i - j).append(newString2);                i += oLength;                j = i;            }            buf.append(line2, j, line2.length - j);            return buf.toString();        }        return line;    }    /**     * Replaces all instances of oldString with newString in line with the     * added feature that matches of newString in oldString ignore case.     * The count paramater is set to the number of replaces performed.     *     * @param line      the String to search to perform replacements on     * @param oldString the String that should be replaced by newString     * @param newString the String that will replace all instances of oldString     * @param count     a value that will be updated with the number of replaces     *                  performed.     * @return a String will all instances of oldString replaced by newString     */    public static String replaceIgnoreCase(String line, String oldString,            String newString, int[] count)    {        if (line == null) {            return null;        }        String lcLine = line.toLowerCase();        String lcOldString = oldString.toLowerCase();        int i = 0;        if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {            int counter = 1;            char[] line2 = line.toCharArray();            char[] newString2 = newString.toCharArray();            int oLength = oldString.length();            StringBuilder buf = new StringBuilder(line2.length);            buf.append(line2, 0, i).append(newString2);            i += oLength;            int j = i;            while ((i = lcLine.indexOf(lcOldString, i)) > 0) {                counter++;                buf.append(line2, j, i - j).append(newString2);                i += oLength;                j = i;            }            buf.append(line2, j, line2.length - j);            count[0] = counter;            return buf.toString();        }        return line;    }    /**     * Replaces all instances of oldString with newString in line.     * The count Integer is updated with number of replaces.     *     * @param line the String to search to perform replacements on.     * @param oldString the String that should be replaced by newString.     * @param newString the String that will replace all instances of oldString.     * @return a String will all instances of oldString replaced by newString.     */    public static String replace(String line, String oldString,            String newString, int[] count)    {        if (line == null) {            return null;        }        int i = 0;        if ((i = line.indexOf(oldString, i)) >= 0) {            int counter = 1;            char[] line2 = line.toCharArray();            char[] newString2 = newString.toCharArray();            int oLength = oldString.length();            StringBuilder buf = new StringBuilder(line2.length);            buf.append(line2, 0, i).append(newString2);            i += oLength;            int j = i;            while ((i = line.indexOf(oldString, i)) > 0) {                counter++;                buf.append(line2, j, i - j).append(newString2);                i += oLength;                j = i;            }            buf.append(line2, j, line2.length - j);            count[0] = counter;            return buf.toString();        }        return line;    }    /**     * This method takes a string and strips out all tags except <br> tags while still leaving     * the tag body intact.     *     * @param in the text to be converted.     * @return the input string with all tags removed.     */    public static String stripTags(String in) {        if (in == null) {            return null;        }        char ch;        int i = 0;        int last = 0;        char[] input = in.toCharArray();        int len = input.length;        StringBuilder out = new StringBuilder((int)(len * 1.3));        for (; i < len; i++) {            ch = input[i];            if (ch > '>') {            }            else if (ch == '<') {                if (i + 3 < len && input[i + 1] == 'b' && input[i + 2] == 'r' && input[i + 3] == '>') {                    i += 3;                    continue;                }                if (i > last) {                    out.append(input, last, i - last);                }                last = i + 1;            }            else if (ch == '>') {                last = i + 1;            }        }        if (last == 0) {            return in;        }        if (i > last) {            out.append(input, last, i - last);        }        return out.toString();    }    /**     * This method takes a string which may contain HTML tags (ie, &lt;b&gt;,     * &lt;table&gt;, etc) and converts the '&lt'' and '&gt;' characters to     * their HTML escape sequences.     *     * @param in the text to be converted.     * @return the input string with the characters '&lt;' and '&gt;' replaced     *         with their HTML escape sequences.     */    public static String escapeHTMLTags(String in) {        if (in == null) {            return null;        }        char ch;        int i = 0;        int last = 0;        char[] input = in.toCharArray();        int len = input.length;        StringBuilder out = new StringBuilder((int)(len * 1.3));        for (; i < len; i++) {            ch = input[i];            if (ch > '>') {            }            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(GT_ENCODE);            }        }        if (last == 0) {            return in;        }        if (i > last) {            out.append(input, last, i - last);        }        return out.toString();    }    /**     * Used by the hash method.     */    private static Map<String, MessageDigest> digests =            new ConcurrentHashMap<String, MessageDigest>();    /**     * 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.     * <p/>     * A hash is a one-way function -- that is, given an     * input, an output is easily computed. However, given the output, the     * input is almost impossible to compute. This is useful for passwords     * since we can store the hash and a hacker will then have a very hard time     * determining the original password.     * <p/>     * In Jive, every time a user logs in, we simply     * take their plain text password, compute the hash, and compare the     * generated hash to the stored hash. Since it is almost impossible that     * two passwords will generate the same hash, we know if the user gave us     * the correct password or not. The only negative to this system is that     * password recovery is basically impossible. Therefore, a reset password     * method is used instead.     *     * @param data the String to compute the hash of.     * @return a hashed version of the passed-in String     */    public static String hash(String data) {        return hash(data, "MD5");    }    /**     * Hashes a String using the specified 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.     * <p/>     * A hash is a one-way function -- that is, given an     * input, an output is easily computed. However, given the output, the     * input is almost impossible to compute. This is useful for passwords     * since we can store the hash and a hacker will then have a very hard time     * determining the original password.     * <p/>     * In Jive, every time a user logs in, we simply     * take their plain text password, compute the hash, and compare the     * generated hash to the stored hash. Since it is almost impossible that     * two passwords will generate the same hash, we know if the user gave us     * the correct password or not. The only negative to this system is that     * password recovery is basically impossible. Therefore, a reset password     * method is used instead.     *     * @param data the String to compute the hash of.     * @param algorithm the name of the algorithm requested.     * @return a hashed version of the passed-in String

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -