|
@@ -1027,6 +1027,44 @@ public final class StringUtils {
|
|
|
throw new NoSuchElementException();
|
|
throw new NoSuchElementException();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Extracts 'length' characters from 'begin'. Both parameters accept negative values.
|
|
|
|
|
+ * Negative value 'x' means "x less than length".
|
|
|
|
|
+ * @param text original string
|
|
|
|
|
+ * @param begin allowed to be negative, then index is counted from the end
|
|
|
|
|
+ * @param length allowed to negative, then length is counted as 'less than original length'
|
|
|
|
|
+ * @return substring
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String substr(String text, int begin, int length) {
|
|
|
|
|
+ if(text == null || length==0) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ if(begin < 0) {
|
|
|
|
|
+ begin += text.length();
|
|
|
|
|
+ }
|
|
|
|
|
+ if(length < 0) {
|
|
|
|
|
+ length += text.length();
|
|
|
|
|
+ }
|
|
|
|
|
+ int b = MathUtils.clip(begin, 0, text.length());
|
|
|
|
|
+ int e = MathUtils.clip(begin + length, 0, text.length());
|
|
|
|
|
+ return text.substring(b, e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Extracts 'length' characters from string. If length is positive, extracts characters from begin.
|
|
|
|
|
+ * If length is negative, extracts characters from the end.
|
|
|
|
|
+ * @param text original text
|
|
|
|
|
+ * @param length number of characters
|
|
|
|
|
+ * @return substring
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String substr(String text, int length) {
|
|
|
|
|
+ if (length < 0) {
|
|
|
|
|
+ return substr(text, length, -length);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return substr(text, 0, length);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private static String replaceEndl(String text) {
|
|
private static String replaceEndl(String text) {
|
|
|
return text.replaceAll("\r\n", "\037").replace('\n', '\037').replace('\r', '\037');
|
|
return text.replaceAll("\r\n", "\037").replace('\n', '\037').replace('\r', '\037');
|
|
|
}
|
|
}
|