Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package net.dongliu.commons.lang;
/**
* string utils methods
*
* @author Dong Liu
*/
public class Strings {
/**
* get sub string by begin(included) and end(not included) index.
* begin and end can be negative, will be eval as str.length() + begin/end.
*
*
* @param str can be null. when str is null, always return null
* @return subString
* @throws ArrayIndexOutOfBoundsException
*/
public static String sub(String str, int begin, int end) {
if (str == null) {
return null;
}
int len = str.length();
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
if (begin >= len || begin < 0) {
throw new ArrayIndexOutOfBoundsException("invalid start index:" + begin);
}
if (end > len || end < 0) {
throw new ArrayIndexOutOfBoundsException("invalid end index:" + end);
}
return str.substring(begin, end);
}
/**
* get sub string by begin(included) index, till the end of string.
* begin can be negative, will be eval as str.length() + begin.
*
*
* @param str can be null. when str is null, always return null
* @return subString
* @throws ArrayIndexOutOfBoundsException
*/
public static String sub(String str, int begin) {
return sub(str, begin, str.length());
}
/**
* get sub string by begin(included) index, till the end of string.
* begin can be negative, will be eval as str.length() + begin.
*
*
* @param str can be null. when str is null, always return null
* @return subString
* @throws ArrayIndexOutOfBoundsException
*/
public static String rSub(String str, int end) {
return sub(str, 0, end);
}
/**
* get sub-string between begin and end.
*
* @param str if null, return null
* @param begin the begin str, cannot be null
* @param end the end str, cannot be null
* @return str between begin and end. if not exists, return null
*/
public static String between(String str, String begin, String end) {
if (str == null) {
return null;
}
int idx = str.indexOf(begin);
if (idx < 0) {
return null;
}
idx += begin.length();
int eidx = str.indexOf(end, idx);
if (eidx < 0) {
return null;
}
return str.substring(idx, eidx);
}
/**
* split str by white characters
*
* "a b c" --> ["a", "b", "c"]
* " a c " --> ["a","c"]
*
*/
public String[] split(String str) {
return str.trim().split("\\w+");
}
/**
* format string
*