📄 stringutils.java
字号:
/**
* $RCSfile: StringUtils.java,v $
* $Revision: 1.11.2.3 $
* $Date: 2001/03/06 16:38:31 $
*
* Copyright (C) 2000 CoolServlets.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* CoolServlets.com (http://www.Yasna.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Jive" and "CoolServlets.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@Yasna.com.
*
* 5. Products derived from this software may not be called "Jive",
* nor may "Jive" appear in their name, without prior written
* permission of CoolServlets.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of CoolServlets.com. For more information
* on CoolServlets.com, please see <http://www.Yasna.com>.
*/
package com.Yasna.util;
import java.security.*;
import java.text.*;
import java.util.*;
/**
* Utility class to peform common String manipulation algorithms.
*/
public class StringUtils {
/**
* Initialization lock for the whole class. Init's only happen once per
* class load so this shouldn't be a bottleneck.
*/
private static Object initLock = new Object();
/**
* Replaces all instances of oldString with newString in line.
*
* @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 final String replace( String line, String oldString, String newString )
{
if (line == null) {
return null;
}
int i=0;
if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, 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.
*
* @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 final 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();
StringBuffer buf = new StringBuffer(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.
* 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 final 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 = 0;
counter++;
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(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 which may contain HTML tags (ie, <b>,
* <table>, etc) and converts the '<'' and '>' characters to
* their HTML escape sequences.
*
* @param input the text to be converted.
* @return the input string with the characters '<' and '>' replaced
* with their HTML escape sequences.
*/
public static final String escapeHTMLTags( String input ) {
//Check if the string is null or zero length -- if so, return
//what was sent in.
if( input == null || input.length() == 0 ) {
return input;
}
//Use a StringBuffer in lieu of String concatenation -- it is
//much more efficient this way.
StringBuffer buf = new StringBuffer(input.length());
char ch = ' ';
for( int i=0; i<input.length(); i++ ) {
ch = input.charAt(i);
if( ch == '<' ) {
buf.append("<");
}
else if( ch == '>' ) {
buf.append(">");
}
else {
buf.append( ch );
}
}
return buf.toString();
}
/**
* Used by the hash method.
*/
private static MessageDigest digest = null;
/**
* 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 synchronized static final String hash(String data) {
if (digest == null) {
try {
digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException nsae) {
System.err.println("Failed to load the MD5 MessageDigest. " +
"Jive will be unable to function normally.");
nsae.printStackTrace();
}
}
//Now, compute hash.
digest.update(data.getBytes());
return toHex(digest.digest());
}
/**
* Turns an array of bytes into a String representing each byte as an
* unsigned hex number.
* <p>
* Method by Santeri Paavolainen, Helsinki Finland 1996<br>
* (c) Santeri Paavolainen, Helsinki Finland 1996<br>
* Distributed under LGPL.
*
* @param hash an rray of bytes to convert to a hex-string
* @return generated hex string
*/
public static final String toHex (byte hash[]) {
StringBuffer buf = new StringBuffer(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++) {
if (((int) hash[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) hash[i] & 0xff, 16));
}
return buf.toString();
}
/**
* Converts a line of text into an array of lower case words. Words are
* delimited by the following characters: , .\r\n:/\+
* <p>
* In the future, this method should be changed to use a
* BreakIterator.wordInstance(). That class offers much more fexibility.
*
* @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];
}
StringTokenizer tokens = new StringTokenizer(text, " ,\r\n.:/\\+");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -