📄 stringutils.java
字号:
* StringUtils.stripStart("abc", null) = "abc" * StringUtils.stripStart(" abc", null) = "abc" * StringUtils.stripStart("abc ", null) = "abc " * StringUtils.stripStart(" abc ", null) = "abc " * StringUtils.stripStart("yxabc ", "xyz") = "abc " * </pre> * * @param str the String to remove characters from, may be null * @param stripChars the characters to remove, null treated as whitespace * @return the stripped String, <code>null</code> if null String input */ public static String stripStart(String str, String stripChars) { int start = getStripStart(str, stripChars); return (start <= 0) ? str : str.substring(start); } private static int getStripStart(String str, String stripChars) { int strLen; if (str == null || (strLen = str.length()) == 0) { return -1; } int start = 0; if (stripChars == null) { while ((start != strLen) && Character.isWhitespace(str.charAt(start))) { start++; } } else if (stripChars.length() == 0) { return start; } else { while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) { start++; } } return start; } /** * <p>Strips any of a set of characters from the end of a String.</p> * * <p>A <code>null</code> input String returns <code>null</code>. * An empty string ("") input returns the empty string.</p> * * <p>If the stripChars String is <code>null</code>, whitespace is * stripped as defined by {@link Character#isWhitespace(char)}.</p> * * <pre> * StringUtils.stripEnd(null, *) = null * StringUtils.stripEnd("", *) = "" * StringUtils.stripEnd("abc", "") = "abc" * StringUtils.stripEnd("abc", null) = "abc" * StringUtils.stripEnd(" abc", null) = " abc" * StringUtils.stripEnd("abc ", null) = "abc" * StringUtils.stripEnd(" abc ", null) = " abc" * StringUtils.stripEnd(" abcyx", "xyz") = " abc" * </pre> * * @param str the String to remove characters from, may be null * @param stripChars the characters to remove, null treated as whitespace * @return the stripped String, <code>null</code> if null String input */ public static String stripEnd(String str, String stripChars) { int end = getStripEnd(str, stripChars); return (end < 0) ? str : str.substring(0, end); } private static int getStripEnd(String str, String stripChars) { int end; if (str == null || (end = str.length()) == 0) { return -1; } if (stripChars == null) { while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) { end--; } } else if (stripChars.length() == 0) { return end; } else { while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) { end--; } } return end; } /** * write the escaped version of a given string * * @param str string to be encoded * @return a new escaped <code>String</code>, <code>null</code> if null string input */ public static String escapeNumericChar(String str) { if (str == null) { return null; } try { StringWriter writer = new StringWriter(str.length()); escapeNumericChar(writer, str); return writer.toString(); } catch (IOException ioe) { // this should never ever happen while writing to a StringWriter ioe.printStackTrace(); return null; } } /** * write the escaped version of a given string * * @param out writer to write this string to * @param str string to be encoded */ public static void escapeNumericChar(Writer out, String str) throws IOException { if (str == null) { return; } int length = str.length(); char character; for (int i = 0; i < length; i++) { character = str.charAt( i ); if (character > 0x7F) { out.write("&#x"); out.write(Integer.toHexString(character).toUpperCase()); out.write(";"); } else { out.write(character); } } } /** * <p>Unescapes numeric character referencs found in the <code>String</code>.</p> * * <p>For example, it will return a unicode string which means the specified numeric * character references looks like "ようこそ".</p> * * @param str the <code>String</code> to unescape, may be null * @return a new unescaped <code>String</code>, <code>null</code> if null string input */ public static String unescapeNumericChar(String str) { if (str == null) { return null; } try { StringWriter writer = new StringWriter(str.length()); unescapeNumericChar(writer, str); return writer.toString(); } catch (IOException ioe) { // this should never ever happen while writing to a StringWriter ioe.printStackTrace(); return null; } } /** * <p>Unescapes numeric character references found in the <code>String</code> to a * <code>Writer</code>.</p> * * <p>For example, it will return a unicode string which means the specified numeric * character references looks like "ようこそ".</p> * * <p>A <code>null</code> string input has no effect.</p> * * @param out the <code>Writer</code> used to output unescaped characters * @param str the <code>String</code> to unescape, may be null * @throws IllegalArgumentException if the Writer is <code>null</code> * @throws java.io.IOException if error occurs on underlying Writer */ public static void unescapeNumericChar(Writer out, String str) throws IOException { if (out == null) { throw new IllegalArgumentException("The Writer must not be null"); } if (str == null) { return; } int sz = str.length(); StringBuffer unicode = new StringBuffer(4); StringBuffer escapes = new StringBuffer(3); boolean inUnicode = false; for (int i = 0; i < sz; i++) { char ch = str.charAt(i); if (inUnicode) { // if in unicode, then we're reading unicode // values in somehow unicode.append(ch); if (unicode.length() == 4) { // unicode now contains the four hex digits // which represents our unicode character try { int value = Integer.parseInt(unicode.toString(), 16); out.write((char) value); unicode.setLength(0); // need to skip the delimiter - ';' i = i + 1; inUnicode = false; } catch (NumberFormatException nfe) { throw new InternalException(nfe); } } continue; } else if (ch=='&') { // Start of the escape sequence ... // At least, the numeric character references require 8 bytes to // describe a Unicode character like as"" if (i+7 <= sz) { escapes.append(ch); escapes.append(str.charAt(i+1)); escapes.append(str.charAt(i+2)); if (escapes.toString().equals("&#x") && str.charAt(i+7)==';') { inUnicode = true; } else { out.write(escapes.toString()); } escapes.setLength(0); // need to skip the escaping chars - '&#x' i = i + 2; } else { out.write(ch); } continue; } out.write(ch); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -