📄 strings.java
字号:
// Read the xxxx int value = 0; for (int i = 0; i < 4; i++) { aChar = in[off++]; switch (aChar) { case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : value = (value << 4) + aChar - '0'; break; case 'a' : case 'b' : case 'c' : case 'd' : case 'e' : case 'f' : value = (value << 4) + 10 + aChar - 'a'; break; case 'A' : case 'B' : case 'C' : case 'D' : case 'E' : case 'F' : value = (value << 4) + 10 + aChar - 'A'; break; default : throw new IllegalArgumentException("Malformed \\uxxxx encoding."); } } out[outLen++] = (char)value; } else { if (aChar == 't') { aChar = '\t'; } else if (aChar == 'r') { aChar = '\r'; } else if (aChar == 'n') { aChar = '\n'; } else if (aChar == 'f') { aChar = '\f'; } out[outLen++] = aChar; } } else { out[outLen++] = aChar; } } return new String(out, 0, outLen); } /** * Checks whether the <code>string</code> is considered empty. Empty means that the string may * contain whitespace, but no visible characters. * * "\n\t " is considered empty, while " a" is not. * * @param string * The string * @return True if the string is null or "" */ public static boolean isEmpty(final CharSequence string) { return string == null || string.length() == 0 || string.toString().trim().length() == 0; } /** * Checks whether two strings are equals taken care of 'null' values and treating 'null' same as * trim(string).equals("") * * @param string1 * @param string2 * @return true, if both strings are equal */ public static boolean isEqual(final String string1, final String string2) { if ((string1 == null) && (string2 == null)) { return true; } if (isEmpty(string1) && isEmpty(string2)) { return true; } if (string1 == null || string2 == null) { return false; } return string1.equals(string2); } /** * Converts the text in <code>s</code> to a corresponding boolean. On, yes, y, true and 1 are * converted to <code>true</code>. Off, no, n, false and 0 (zero) are converted to * <code>false</code>. An empty string is converted to <code>false</code>. Conversion is * case-insensitive, and does <em>not</em> take internationalization into account. * * 'Ja', 'Oui', 'Igen', 'Nein', 'Nee', 'Non', 'Nem' are all illegal values. * * @param s * the value to convert into a boolean * @return Boolean the converted value of <code>s</code> * @throws StringValueConversionException * when the value of <code>s</code> is not recognized. */ public static boolean isTrue(final String s) throws StringValueConversionException { if (s != null) { if (s.equalsIgnoreCase("true")) { return true; } if (s.equalsIgnoreCase("false")) { return false; } if (s.equalsIgnoreCase("on") || s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") || s.equalsIgnoreCase("1")) { return true; } if (s.equalsIgnoreCase("off") || s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") || s.equalsIgnoreCase("0")) { return false; } if (isEmpty(s)) { return false; } throw new StringValueConversionException("Boolean value \"" + s + "\" not recognized"); } return false; } /** * Joins string fragments using the specified separator * * @param separator * @param fragments * @return combined fragments */ public static String join(String separator, String[] fragments) { if (fragments.length < 1) { // no elements return ""; } else if (fragments.length < 2) { // single element return fragments[0]; } else { // two or more elements StringBuffer buff = new StringBuffer(128); if (fragments[0] != null) { buff.append(fragments[0]); } for (int i = 1; i < fragments.length; i++) { if ((fragments[i - 1] != null) || (fragments[i] != null)) { boolean lhsClosed = fragments[i - 1].endsWith(separator); boolean rhsClosed = fragments[i].startsWith(separator); if (lhsClosed && rhsClosed) { buff.append(fragments[i].substring(1)); } else if (!lhsClosed && !rhsClosed) { buff.append(separator).append(fragments[i]); } else { buff.append(fragments[i]); } } } return buff.toString(); } } /** * Gets the last path component of a path using a given separator. If the separator cannot be * found, the path itself is returned. * <p> * For example, lastPathComponent("foo.bar", '.') would return "bar" and * lastPathComponent("foo", '.') would return "foo". * * @param path * The path to parse * @param separator * The path separator character * @return The last component in the path or path itself if no separator characters exist. */ public static String lastPathComponent(final String path, final char separator) { if (path == null) { return null; } final int index = path.lastIndexOf(separator); if (index == -1) { return path; } return path.substring(index + 1); } /** * Replace all occurrences of one string replaceWith another string. * * @param s * The string to process * @param searchFor * The value to search for * @param replaceWith * The value to searchFor replaceWith * @return The resulting string with searchFor replaced with replaceWith */ public static CharSequence replaceAll(final CharSequence s, final CharSequence searchFor, CharSequence replaceWith) { if (s == null) { return null; } // If searchFor is null or the empty string, then there is nothing to // replace, so returning s is the only option here. if (searchFor == null || "".equals(searchFor)) { return s; } // If replaceWith is null, then the searchFor should be replaced with // nothing, which can be seen as the empty string. if (replaceWith == null) { replaceWith = ""; } String searchString = searchFor.toString(); // Look for first occurrence of searchFor int matchIndex = search(s, searchString, 0); if (matchIndex == -1) { // No replace operation needs to happen return s; } else { // Allocate a AppendingStringBuffer that will hold one replacement // with a // little extra room. int size = s.length(); final int replaceWithLength = replaceWith.length(); final int searchForLength = searchFor.length(); if (replaceWithLength > searchForLength) { size += (replaceWithLength - searchForLength); } final AppendingStringBuffer buffer = new AppendingStringBuffer(size + 16); int pos = 0; do { // Append text up to the match` append(buffer, s, pos, matchIndex); // Add replaceWith text buffer.append(replaceWith); // Find next occurrence, if any pos = matchIndex + searchForLength; matchIndex = search(s, searchString, pos); } while (matchIndex != -1); // Add tail of s buffer.append(s.subSequence(pos, s.length())); // Return processed buffer return buffer; } } /** * Replace HTML numbers like 值 by the appropriate character. * * @param str * The text to be evaluated * @return The text with "numbers" replaced */ public static String replaceHtmlEscapeNumber(String str) { if (str == null) { return null; } Matcher matcher = htmlNumber.matcher(str); while (matcher.find()) { int pos = matcher.start(); int end = matcher.end(); int number = Integer.parseInt(str.substring(pos + 2, end - 1)); char ch = (char)number; str = str.substring(0, pos) + ch + str.substring(end); matcher = htmlNumber.matcher(str); } return str; } /** * Simpler, faster version of String.split() for splitting on a simple character. * * @param s * The string to split * @param c * The character to split on * @return The array of strings */ public static String[] split(final String s, final char c) { if (s == null) { return new String[0]; } final List strings = new ArrayList(); int pos = 0; while (true) { int next = s.indexOf(c, pos); if (next == -1) { strings.add(s.substring(pos)); break; } else { strings.add(s.substring(pos, next)); } pos = next + 1; } final String[] result = new String[strings.size()]; strings.toArray(result); return result; } /** * Strips the ending from the string <code>s</code>. * * @param s * The string to strip * @param ending * The ending to strip off * @return The stripped string or the original string if the ending did not exist */ public static String stripEnding(final String s, final String ending) { if (s == null) { return null; } // Stripping a null or empty string from the end returns the // original string. if (ending == null || "".equals(ending)) { return s; } final int endingLength = ending.length(); final int sLength = s.length(); // When the length of the ending string is larger // than the original string, the original string is returned. if (endingLength > sLength) { return s; } final int index = s.lastIndexOf(ending); final int endpos = sLength - endingLength; if (index == endpos) { return s.substring(0, endpos); } return s; } /** * Strip any jsessionid and possibly other redundant info that might be in our way. * * @param url * The url to strip * @return The stripped url */ public static String stripJSessionId(CharSequence url) { if (url == null) { return null; } StringBuffer path = new StringBuffer(url.toString()); int ixSemiColon = path.indexOf(";"); // strip off any jsession id if (ixSemiColon != -1) { int ixEnd = path.indexOf("?"); if (ixEnd == -1) { ixEnd = path.length(); } path.delete(ixSemiColon, ixEnd); } return path.toString();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -