📄 parsetext.java
字号:
* and breaking the search at the index specified by <code>breakAtIndex</code>.
* <p>
* The position specified by <code>breakAtIndex</code> is not included in the search.
* <p>
* If the search is to continue to the end of the text,
* the value {@link #NO_BREAK ParseText.NO_BREAK} should be specified as the <code>breakAtIndex</code>.
* <p>
* If the specified string is not found then -1 is returned.
*
* @param searchString a string.
* @param fromIndex the index to start the search from.
* @param breakAtIndex the index at which to break off the search, or {@link #NO_BREAK} if the search is to continue to the end of the text.
* @return the index within this parse text of the first occurrence of the specified string within the specified range, or -1 if the string is not found.
*/
public int indexOf(final String searchString, final int fromIndex, final int breakAtIndex) {
return (searchString.length()==1)
? indexOf(searchString.charAt(0),fromIndex,breakAtIndex)
: indexOf(searchString.toCharArray(),fromIndex,breakAtIndex);
}
/**
* Returns the index within this parse text of the first occurrence of the specified character array,
* starting the search at the position specified by <code>fromIndex</code>,
* and breaking the search at the index specified by <code>breakAtIndex</code>.
* <p>
* The position specified by <code>breakAtIndex</code> is not included in the search.
* <p>
* If the search is to continue to the end of the text,
* the value {@link #NO_BREAK ParseText.NO_BREAK} should be specified as the <code>breakAtIndex</code>.
* <p>
* If the specified character array is not found then -1 is returned.
*
* @param searchCharArray a character array.
* @param fromIndex the index to start the search from.
* @param breakAtIndex the index at which to break off the search, or {@link #NO_BREAK} if the search is to continue to the end of the text.
* @return the index within this parse text of the first occurrence of the specified character array within the specified range, or -1 if the character array is not found.
*/
public int indexOf(final char[] searchCharArray, final int fromIndex, final int breakAtIndex) {
if (searchCharArray.length==0) return fromIndex;
final char firstChar=searchCharArray[0];
final int lastPossibleBreakAtIndex=text.length-searchCharArray.length+1;
final int actualBreakAtIndex=(breakAtIndex==NO_BREAK || breakAtIndex>lastPossibleBreakAtIndex) ? lastPossibleBreakAtIndex : breakAtIndex;
outerLoop: for (int i=(fromIndex<0 ? 0 : fromIndex); i<actualBreakAtIndex; i++) {
if (text[i]==firstChar) {
for (int j=1; j<searchCharArray.length; j++)
if (searchCharArray[j]!=text[j+i]) continue outerLoop;
return i;
}
}
return -1;
}
/**
* Returns the index within this parse text of the last occurrence of the specified string,
* searching backwards starting at the position specified by <code>fromIndex</code>.
* <p>
* If the specified string is not found then -1 is returned.
*
* @param searchString a string.
* @param fromIndex the index to start the search from.
* @return the index within this parse text of the last occurrence of the specified string within the specified range, or -1 if the string is not found.
*/
public int lastIndexOf(final String searchString, final int fromIndex) {
return (searchString.length()==1)
? lastIndexOf(searchString.charAt(0),fromIndex,NO_BREAK)
: lastIndexOf(searchString.toCharArray(),fromIndex,NO_BREAK);
}
/**
* Returns the index within this parse text of the last occurrence of the specified character array,
* searching backwards starting at the position specified by <code>fromIndex</code>.
* <p>
* If the specified character array is not found then -1 is returned.
*
* @param searchCharArray a character array.
* @param fromIndex the index to start the search from.
* @return the index within this parse text of the last occurrence of the specified character array within the specified range, or -1 if the character array is not found.
*/
public int lastIndexOf(final char[] searchCharArray, final int fromIndex) {
return lastIndexOf(searchCharArray,fromIndex,NO_BREAK);
}
/**
* Returns the index within this parse text of the last occurrence of the specified string,
* searching backwards starting at the position specified by <code>fromIndex</code>,
* and breaking the search at the index specified by <code>breakAtIndex</code>.
* <p>
* The position specified by <code>breakAtIndex</code> is not included in the search.
* <p>
* If the search is to continue to the start of the text,
* the value {@link #NO_BREAK ParseText.NO_BREAK} should be specified as the <code>breakAtIndex</code>.
* <p>
* If the specified string is not found then -1 is returned.
*
* @param searchString a string.
* @param fromIndex the index to start the search from.
* @param breakAtIndex the index at which to break off the search, or {@link #NO_BREAK} if the search is to continue to the start of the text.
* @return the index within this parse text of the last occurrence of the specified string within the specified range, or -1 if the string is not found.
*/
public int lastIndexOf(final String searchString, final int fromIndex, final int breakAtIndex) {
return (searchString.length()==1)
? lastIndexOf(searchString.charAt(0),fromIndex,breakAtIndex)
: lastIndexOf(searchString.toCharArray(),fromIndex,breakAtIndex);
}
/**
* Returns the index within this parse text of the last occurrence of the specified character array,
* searching backwards starting at the position specified by <code>fromIndex</code>,
* and breaking the search at the index specified by <code>breakAtIndex</code>.
* <p>
* The position specified by <code>breakAtIndex</code> is not included in the search.
* <p>
* If the search is to continue to the start of the text,
* the value {@link #NO_BREAK ParseText.NO_BREAK} should be specified as the <code>breakAtIndex</code>.
* <p>
* If the specified character array is not found then -1 is returned.
*
* @param searchCharArray a character array.
* @param fromIndex the index to start the search from.
* @param breakAtIndex the index at which to break off the search, or {@link #NO_BREAK} if the search is to continue to the start of the text.
* @return the index within this parse text of the last occurrence of the specified character array within the specified range, or -1 if the character array is not found.
*/
public int lastIndexOf(final char[] searchCharArray, int fromIndex, final int breakAtIndex) {
if (searchCharArray.length==0) return fromIndex;
final int rightIndex=text.length-searchCharArray.length;
if (breakAtIndex>rightIndex) return -1;
if (fromIndex>rightIndex) fromIndex=rightIndex;
final int lastCharIndex=searchCharArray.length-1;
final char lastChar=searchCharArray[lastCharIndex];
final int actualBreakAtPos=breakAtIndex+lastCharIndex;
outerLoop: for (int i=fromIndex+lastCharIndex; i>actualBreakAtPos; i--) {
if (text[i]==lastChar) {
final int startIndex=i-lastCharIndex;
for (int j=lastCharIndex-1; j>=0; j--)
if (searchCharArray[j]!=text[j+startIndex]) continue outerLoop;
return startIndex;
}
}
return -1;
}
/**
* Returns the length of the parse text.
* @return the length of the parse text.
*/
public int length() {
return text.length;
}
/**
* Returns a new string that is a substring of this parse text.
* <p>
* The substring begins at the specified <code>beginIndex</code> and extends to the character at index <code>endIndex</code> - 1.
* Thus the length of the substring is <code>endIndex-beginIndex</code>.
*
* @param beginIndex the begin index, inclusive.
* @param endIndex the end index, exclusive.
* @return a new string that is a substring of this parse text.
*/
public String substring(final int beginIndex, final int endIndex) {
return new String(text,beginIndex,endIndex-beginIndex);
}
/**
* Returns a new character sequence that is a subsequence of this sequence.
* <p>
* This is equivalent to {@link #substring(int,int) substring(beginIndex,endIndex)}.
*
* @param beginIndex the begin index, inclusive.
* @param endIndex the end index, exclusive.
* @return a new character sequence that is a subsequence of this sequence.
*/
public CharSequence subSequence(final int beginIndex, final int endIndex) {
return substring(beginIndex,endIndex);
}
/**
* Returns the content of the parse text as a <code>String</code>.
* @return the content of the parse text as a <code>String</code>.
*/
public String toString() {
return new String(text);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -