mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
update StringUtils.java
add Method subString("abcdc","a","c",true)="bcd"
This commit is contained in:
@@ -60,6 +60,33 @@ public class StringUtils {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: subString("abcdc","a","c",true)="bcd"
|
||||
*
|
||||
* @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);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param in
|
||||
* @return
|
||||
|
||||
Reference in New Issue
Block a user