📄 stringutils.java
字号:
// }//// return toReturn.toString();// }// else { regexp.insert(0, "s/\\b("); // The word list is here already, so just append the rest. regexp.append(")\\b/"); regexp.append(startHighlight); regexp.append("$1"); regexp.append(endHighlight); regexp.append("/igm"); // Do the actual substitution via a simple regular expression. return perl5Util.substitute(regexp.toString(), string);// } } /** * 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); } else if (ch == 10 || ch == 13 || ch == 9) { continue; } else if (ch < 32) { // Disallow all ASCII control characters, except space, // enter characters and tabs: if (i > last) { out.append(input, last, i - last); } last = i + 1; } } 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, "&", "&"); } private static final char[] zeroArray = "0000000000000000000000000000000000000000000000000000000000000000".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 * 64. * * @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); } /** * Split a string as an array using whitespace as separator * * @param line The string to be split * @return An array of whitespace-separated tokens */ public static String[] split(String line) { return split(line, " \t\n\r"); } /** * Split a string as an array using a given set of separators * * @param line The string to be split * @param delimiter A string containing token separators * @return An array of token */ public static String[] split(String line, String delimiter) { return Tokenizer.tokenize(line, delimiter, false); } /** * Tests whether a given character is alphabetic, numeric or * underscore * * @param c The character to be tested * @return whether the given character is alphameric or not */ public static boolean isAlphaNumeric(char c) { return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } /** * Counts the occurrence of the given char in the string. * * @param str The string to be tested * @param c the char to be counted * @return the occurrence of the character in the string. */ public static int count(String str, char c) { int index = 0; char[] chars = str.toCharArray(); for (int i = 0; i < chars.length; i++) { if (chars[i] == c) index++; } return index; } /** * Matches two strings. * * @param a The first string * @param b The second string * @return the index where the two strings stop matching starting from 0 */ public static int matchStrings(String a, String b) { int i; char[] ca = a.toCharArray(); char[] cb = b.toCharArray(); int len = ( ca.length < cb.length ) ? ca.length : cb.length; for (i = 0; i < len; i++) { if (ca[i] != cb[i]) break; } return i; } /** * Replaces tokens in input with Value present in System.getProperty */ public static String replaceToken(String s) { int startToken = s.indexOf("${"); int endToken = s.indexOf("}",startToken); String token = s.substring(startToken+2,endToken); StringBuffer value = new StringBuffer(); value.append(s.substring(0,startToken)); value.append(System.getProperty(token)); value.append(s.substring(endToken+1)); return value.toString(); } // Testing method /* public static void main(String[] args) { { String test1 = "The quick brown fox jumped"; int chop11 = test1.length(); int chop12 = test1.length() - 1; int chop13 = test1.length() - 3; int chop14 = test1.length() - "jumped".length(); int chop15 = test1.length() - "ox jumped".length(); // run test 1 String result11 = chopAtWord(test1, chop11); String result12 = chopAtWord(test1, chop12); String result13 = chopAtWord(test1, chop13); String result14 = chopAtWord(test1, chop14); String result15 = chopAtWord(test1, chop15); // print results if (test1.equals(result11)) { System.err.println("Test 1.1 passed, result: " + result11); } else { System.err.println("Test 1.1 failed, result: " + result11); } if ("The quick brown fox".equals(result12)) { System.err.println("Test 1.2 passed, result: " + result12); } else { System.err.println("Test 1.2 failed, result: " + result12); } if ("The quick brown fox".equals(result13)) { System.err.println("Test 1.3 passed, result: " + result13); } else { System.err.println("Test 1.3 failed, result: " + result13); } if ("The quick brown fox".equals(result14)) { System.err.println("Test 1.4 passed, result: " + result14); } else { System.err.println("Test 1.4 failed, result: " + result14); } if ("The quick brown".equals(result15)) { System.err.println("Test 1.5 passed, result: " + result15); } else { System.err.println("Test 1.5 failed, result: " + result15); } } System.err.println(""); { String test2 = "The quick brown fox jumped"; int chop21 = test2.length(); int chop22 = test2.length() - 1; int chop23 = test2.length() - 3; int chop24 = test2.length() - "jumped".length(); int chop25 = test2.length() - "ox jumped".length(); // run test 1 String result21 = chopAtWord(test2, chop21, 0); String result22 = chopAtWord(test2, chop22, 0); String result23 = chopAtWord(test2, chop23, 0); String result24 = chopAtWord(test2, chop24, 0); String result25 = chopAtWord(test2, chop25, 0); // print results if (test2.equals(result21)) { System.err.println("Test 2.1 passed, result: " + result21); } else { System.err.println("Test 2.1 failed, result: " + result21); } if ("The quick brown fox".equals(result22)) { System.err.println("Test 2.2 passed, result: " + result22); } else { System.err.println("Test 2.2 failed, result: " + result22); } if ("The quick brown fox".equals(result23)) { System.err.println("Test 2.3 passed, result: " + result23); } else { System.err.println("Test 2.3 failed, result: " + result23); } if ("The quick brown fox".equals(result24)) { System.err.println("Test 2.4 passed, result: " + result24); } else { System.err.println("Test 2.4 failed, result: " + result24); } if ("The quick brown".equals(result25)) { System.err.println("Test 2.5 passed, result: " + result25); } else { System.err.println("Test 2.5 failed, result: " + result25); } } System.err.println(""); { String test3 = "Thequickbrownfoxjumped"; int chop31 = test3.length(); int chop32 = test3.length() - 1; int chop33 = test3.length() - 3; int chop34 = test3.length() - "jumped".length(); int chop35 = test3.length() - "ox jumped".length(); // run test 1 String result31 = chopAtWord(test3, chop31, "Thequickbrownfoxjumped".length()); String result32 = chopAtWord(test3, chop32, "Thequick".length()); String result33 = chopAtWord(test3, chop33, "Thequick".length()); String result34 = chopAtWord(test3, chop34, "Thequick".length()); String result35 = chopAtWord(test3, chop35, "Thequick".length()); // print results if ("Thequick".equals(result31)) { System.err.println("Test 3.1 passed, result: " + result31); } else { System.err.println("Test 3.1 failed, result: " + result31); } if ("Thequick".equals(result32)) { System.err.println("Test 3.2 passed, result: " + result32); } else { System.err.println("Test 3.2 failed, result: " + result32); } if ("Thequick".equals(result33)) { System.err.println("Test 3.3 passed, result: " + result33); } else { System.err.println("Test 3.3 failed, result: " + result33); } if ("Thequick".equals(result34)) { System.err.println("Test 3.4 passed, result: " + result34); } else { System.err.println("Test 3.4 failed, result: " + result34); } if ("Thequick".equals(result35)) { System.err.println("Test 3.5 passed, result: " + result35); } else { System.err.println("Test 3.5 failed, result: " + result35); } } System.err.println(""); { String test4 = "Java.Lang.ClassNotFoundException:com.Citrix"; int length = test4.length()-3; int min = 20; String result = chopAtWord(test4, length, min); System.err.println("result: " + result); } } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -