Today, when looking at Druid's source code, accidentally saw StringUtil
inside subString method quite interesting thought that says this method can mimic algorithm when the game
first on the code:
/**
* Example: subString("abcdc","a","c",true)="bcd"
* 第四个参数表示是否到最后的一个c 这例子中 若为false则 结果为 b
* @param src
* @param start null while start from index=0
* @param to null while to index=src.length
* @param toLast true while to index=src.lastIndexOf(to)
* @return
*/
public static String subString(String src, String start, String to, boolean toLast) {
if(!toLast) {
return subString(src, start, to);
}
int indexFrom = start == null ? 0 : src.indexOf(start);
int indexTo = to == null ? src.length() : src.lastIndexOf(to);
if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
return null;
}
if (null != start) {
indexFrom += start.length();
}
return src.substring(indexFrom, indexTo);
}
As examples
Example: subString ( "abcdc",
"a", "c", true) = "bcd" fourth parameter to the last one indicates whether c is false this example, if the result is b
then
Example: subString ( "abcdc", "ab",
"c", true) = "cd" then
Example: subString ( "abcdc", "bc", "c", true) = "d"
Algorithm obvious first determine whether the fourth argument is false (false representation than the last "to")
If this argument is false jump following overloaded methods 2
and then give indexFrom indexTo were assigned to this well understood
in the following judgment null ! = start if you let indexFrom become established plus the length of the start position
meaning that is: remove the "start" position and before the letter
if not the only plus this judgment from "start" to start taking subString and contains the start letters
such StringUtils.subString ( "abcdc", "b ", "c", true) = "bcd"
only from the beginning and comprising b b
StringUtils.subString ( "dbcdc", "DB", "C", to true) = "dbcd"
will start from the db and contains db
In addition:
found end i.e. if the start and given to the same and there is still a problem at the beginning of the string is the same and the fourth parameter is not found to false
as StringUtils.subString ( "efcfee", "e ", "e", false);
will be reported java.lang.StringIndexOutOfBoundsException: begin 1, end 0,
length 6 ponder found that
since the beginning of the end of the same and does not explain the "to" e was the last he would think "to" is the first e i.e. end = 0, start = 1 ( end = 0, start = 1 because the above said indexFrom + = start.length ();)
attached: overloaded method 2
/**
* Example: subString("abcd","a","c")="b"
*
* @param src
* @param start null while start from index=0
* @param to null while to index=src.length
* @return
*/
public static String subString(String src, String start, String to) {
int indexFrom = start == null ? 0 : src.indexOf(start);
int indexTo = to == null ? src.length() : src.indexOf(to);
if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
return null;
}
if (null != start) {
indexFrom += start.length();
}
return src.substring(indexFrom, indexTo);
}