📄 xstring.java
字号:
* <i>k</i> such that: * <blockquote><pre> * this.startsWith(str, <i>k</i>) * </pre></blockquote> * is <code>true</code>. * * @param str any string. * @return if the string argument occurs as a substring within this * object, then the index of the first character of the first * such substring is returned; if it does not occur as a * substring, <code>-1</code> is returned. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. */ public int indexOf(XMLString str) { return str().indexOf(str.toString()); } /** * Returns the index within this string of the first occurrence of the * specified substring, starting at the specified index. The integer * returned is the smallest value <i>k</i> such that: * <blockquote><pre> * this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex) * </pre></blockquote> * is <code>true</code>. * <p> * There is no restriction on the value of <code>fromIndex</code>. If * it is negative, it has the same effect as if it were zero: this entire * string may be searched. If it is greater than the length of this * string, it has the same effect as if it were equal to the length of * this string: <code>-1</code> is returned. * * @param str the substring to search for. * @param fromIndex the index to start the search from. * @return If the string argument occurs as a substring within this * object at a starting index no smaller than * <code>fromIndex</code>, then the index of the first character * of the first such substring is returned. If it does not occur * as a substring starting at <code>fromIndex</code> or beyond, * <code>-1</code> is returned. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code> */ public int indexOf(String str, int fromIndex) { return str().indexOf(str, fromIndex); } /** * Returns the index within this string of the rightmost occurrence * of the specified substring. The rightmost empty string "" is * considered to occur at the index value <code>this.length()</code>. * The returned index is the largest value <i>k</i> such that * <blockquote><pre> * this.startsWith(str, k) * </pre></blockquote> * is true. * * @param str the substring to search for. * @return if the string argument occurs one or more times as a substring * within this object, then the index of the first character of * the last such substring is returned. If it does not occur as * a substring, <code>-1</code> is returned. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. */ public int lastIndexOf(String str) { return str().lastIndexOf(str); } /** * Returns the index within this string of the last occurrence of * the specified substring. * * @param str the substring to search for. * @param fromIndex the index to start the search from. There is no * restriction on the value of fromIndex. If it is greater than * the length of this string, it has the same effect as if it * were equal to the length of this string: this entire string * may be searched. If it is negative, it has the same effect * as if it were -1: -1 is returned. * @return If the string argument occurs one or more times as a substring * within this object at a starting index no greater than * <code>fromIndex</code>, then the index of the first character of * the last such substring is returned. If it does not occur as a * substring starting at <code>fromIndex</code> or earlier, * <code>-1</code> is returned. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. */ public int lastIndexOf(String str, int fromIndex) { return str().lastIndexOf(str, fromIndex); } /** * Returns a new string that is a substring of this string. The * substring begins with the character at the specified index and * extends to the end of this string. <p> * Examples: * <blockquote><pre> * "unhappy".substring(2) returns "happy" * "Harbison".substring(3) returns "bison" * "emptiness".substring(9) returns "" (an empty string) * </pre></blockquote> * * @param beginIndex the beginning index, inclusive. * @return the specified substring. * @exception IndexOutOfBoundsException if * <code>beginIndex</code> is negative or larger than the * length of this <code>String</code> object. */ public XMLString substring(int beginIndex) { return new XString(str().substring(beginIndex)); } /** * Returns a new string that is a substring of this string. The * substring begins at the specified <code>beginIndex</code> and * extends to the character at index <code>endIndex - 1</code>. * Thus the length of the substring is <code>endIndex-beginIndex</code>. * * @param beginIndex the beginning index, inclusive. * @param endIndex the ending index, exclusive. * @return the specified substring. * @exception IndexOutOfBoundsException if the * <code>beginIndex</code> is negative, or * <code>endIndex</code> is larger than the length of * this <code>String</code> object, or * <code>beginIndex</code> is larger than * <code>endIndex</code>. */ public XMLString substring(int beginIndex, int endIndex) { return new XString(str().substring(beginIndex, endIndex)); } /** * Concatenates the specified string to the end of this string. * * @param str the <code>String</code> that is concatenated to the end * of this <code>String</code>. * @return a string that represents the concatenation of this object's * characters followed by the string argument's characters. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. */ public XMLString concat(String str) { // %REVIEW% Make an FSB here? return new XString(str().concat(str)); } /** * Converts all of the characters in this <code>String</code> to lower * case using the rules of the given <code>Locale</code>. * * @param locale use the case transformation rules for this locale * @return the String, converted to lowercase. * @see java.lang.Character#toLowerCase(char) * @see java.lang.String#toUpperCase(Locale) */ public XMLString toLowerCase(Locale locale) { return new XString(str().toLowerCase(locale)); } /** * Converts all of the characters in this <code>String</code> to lower * case using the rules of the default locale, which is returned * by <code>Locale.getDefault</code>. * <p> * * @return the string, converted to lowercase. * @see java.lang.Character#toLowerCase(char) * @see java.lang.String#toLowerCase(Locale) */ public XMLString toLowerCase() { return new XString(str().toLowerCase()); } /** * Converts all of the characters in this <code>String</code> to upper * case using the rules of the given locale. * @param locale use the case transformation rules for this locale * @return the String, converted to uppercase. * @see java.lang.Character#toUpperCase(char) * @see java.lang.String#toLowerCase(Locale) */ public XMLString toUpperCase(Locale locale) { return new XString(str().toUpperCase(locale)); } /** * Converts all of the characters in this <code>String</code> to upper * case using the rules of the default locale, which is returned * by <code>Locale.getDefault</code>. * * <p> * If no character in this string has a different uppercase version, * based on calling the <code>toUpperCase</code> method defined by * <code>Character</code>, then the original string is returned. * <p> * Otherwise, this method creates a new <code>String</code> object * representing a character sequence identical in length to the * character sequence represented by this <code>String</code> object and * with every character equal to the result of applying the method * <code>Character.toUpperCase</code> to the corresponding character of * this <code>String</code> object. <p> * Examples: * <blockquote><pre> * "Fahrvergnügen".toUpperCase() returns "FAHRVERGNÜGEN" * "Visit Ljubinje!".toUpperCase() returns "VISIT LJUBINJE!" * </pre></blockquote> * * @return the string, converted to uppercase. * @see java.lang.Character#toUpperCase(char) * @see java.lang.String#toUpperCase(Locale) */ public XMLString toUpperCase() { return new XString(str().toUpperCase()); } /** * Removes white space from both ends of this string. * * @return this string, with white space removed from the front and end. */ public XMLString trim() { return new XString(str().trim()); } /** * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition * of whitespace. Refer to <A href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S"> * the definition of <CODE>S</CODE></A> for details. * @param ch Character to check as XML whitespace. * @return =true if <var>ch</var> is XML whitespace; otherwise =false. */ private static boolean isSpace(char ch) { return XMLCharacterRecognizer.isWhiteSpace(ch); // Take the easy way out for now. } /** * Conditionally trim all leading and trailing whitespace in the specified String. * All strings of white space are * replaced by a single space character (#x20), except spaces after punctuation which * receive double spaces if doublePunctuationSpaces is true. * This function may be useful to a formatter, but to get first class * results, the formatter should probably do it's own white space handling * based on the semantics of the formatting object. * * @param trimHead Trim leading whitespace? * @param trimTail Trim trailing whitespace? * @param doublePunctuationSpaces Use double spaces for punctuation? * @return The trimmed string. */ public XMLString fixWhiteSpace(boolean trimHead, boolean trimTail, boolean doublePunctuationSpaces) { // %OPT% !!!!!!! int len = this.length(); char[] buf = new char[len]; this.getChars(0, len, buf, 0); boolean edit = false; int s; for (s = 0; s < len; s++) { if (isSpace(buf[s])) { break; } } /* replace S to ' '. and ' '+ -> single ' '. */ int d = s; boolean pres = false; for (; s < len; s++) { char c = buf[s]; if (isSpace(c)) { if (!pres) { if (' ' != c) { edit = true; } buf[d++] = ' '; if (doublePunctuationSpaces && (s != 0)) { char prevChar = buf[s - 1]; if (!((prevChar == '.') || (prevChar == '!') || (prevChar == '?'))) { pres = true; } } else { pres = true; } } else { edit = true; pres = true; } } else { buf[d++] = c; pres = false; } } if (trimTail && 1 <= d && ' ' == buf[d - 1]) { edit = true; d--; } int start = 0; if (trimHead && 0 < d && ' ' == buf[0]) { edit = true; start++; } XMLStringFactory xsf = XMLStringFactoryImpl.getFactory(); return edit ? xsf.newstr(new String(buf, start, d - start)) : this; } /** * @see com.sun.org.apache.xpath.internal.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor) */ public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) { visitor.visitStringLiteral(owner, this); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -