📄 stringutil.java
字号:
package net.xdevelop.util;import java.util.*;public class StringUtil{ /** * 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(); } /** * 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.:/\\+"); String [] words = new String[tokens.countTokens()]; for (int i=0; i<words.length; i++) { words[i] = tokens.nextToken().toLowerCase(); } return words; } /** * A list of some of the most common words. For searching and indexing, we * often want to filter out these words since they just confuse searches. * The list was not created scientifically so may be incomplete :) */ private static final String [] commonWords = new String [] { "a", "and", "as", "at", "be", "do", "i", "if", "in", "is", "it", "so", "the", "to" }; private static Map commonWordsMap = null; /** * Returns a new String array with some of the most common English words * removed. The specific words removed are: a, and, as, at, be, do, i, if, * in, is, it, so, the, to */ public static final String [] removeCommonWords(String [] words) { //See if common words map has been initialized. We don't statically //initialize it to save some memory. Even though this a small savings, //it adds up with hundreds of classes being loaded. if (commonWordsMap == null) { synchronized(initLock) { if (commonWordsMap == null) { commonWordsMap = new HashMap(); for (int i=0; i<commonWords.length; i++) { commonWordsMap.put(commonWords[i], commonWords[i]); } } } } //Now, add all words that aren't in the common map to results ArrayList results = new ArrayList(words.length); for (int i=0; i<words.length; i++) { if (!commonWordsMap.containsKey(words[i])) { results.add(words[i]); } } return (String[])results.toArray(new String[results.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 = null; /** * 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 = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -