|
@@ -19,6 +19,8 @@ public final class StringUtils {
|
|
|
|
|
|
|
|
private static final Pattern REGEX_SPECIAL = Pattern.compile("([\\\\*+\\[\\](){}\\$.?\\^|])");
|
|
private static final Pattern REGEX_SPECIAL = Pattern.compile("([\\\\*+\\[\\](){}\\$.?\\^|])");
|
|
|
|
|
|
|
|
|
|
+ private static final CharSequence[] EMPTY = new CharSequence[0];
|
|
|
|
|
+
|
|
|
private StringUtils() { /* utility class */ }
|
|
private StringUtils() { /* utility class */ }
|
|
|
|
|
|
|
|
public static String remove(String text, String fragment) {
|
|
public static String remove(String text, String fragment) {
|
|
@@ -41,6 +43,49 @@ public final class StringUtils {
|
|
|
}
|
|
}
|
|
|
return new String(buffer, 0, position);
|
|
return new String(buffer, 0, position);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static String removePrefix(String text, String prefix) {
|
|
|
|
|
+ if( Strings.isEmpty(text)) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ if(text.startsWith(prefix)) {
|
|
|
|
|
+ return text.substring(prefix.length());
|
|
|
|
|
+ }
|
|
|
|
|
+ return text;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String removeSuffix(String text, String suffix) {
|
|
|
|
|
+ if( Strings.isEmpty(text)) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ if(text.endsWith(suffix)) {
|
|
|
|
|
+ return text.substring(0, text.length() - suffix.length());
|
|
|
|
|
+ }
|
|
|
|
|
+ return text;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String condense(String text, char value) {
|
|
|
|
|
+ final int size = text.length();
|
|
|
|
|
+ final char buffer[] = new char[size];
|
|
|
|
|
+ char c;
|
|
|
|
|
+ boolean pc = false;
|
|
|
|
|
+ int position = 0;
|
|
|
|
|
+ for(int i=0; i<size; i++) {
|
|
|
|
|
+ c = text.charAt(i);
|
|
|
|
|
+ if(c == value) {
|
|
|
|
|
+ if(!pc) {
|
|
|
|
|
+ buffer[position++] = c;
|
|
|
|
|
+ pc = true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // skip character
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ buffer[position++] = c;
|
|
|
|
|
+ pc = false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return new String(buffer, 0, position);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public static String ltrim(String source) {
|
|
public static String ltrim(String source) {
|
|
|
int n = source.length(), offset = 0;
|
|
int n = source.length(), offset = 0;
|
|
@@ -100,8 +145,7 @@ public final class StringUtils {
|
|
|
|
|
|
|
|
public static <T extends CharSequence> String join(Iterable<T> values, String separator) {
|
|
public static <T extends CharSequence> String join(Iterable<T> values, String separator) {
|
|
|
if (values instanceof Collection) {
|
|
if (values instanceof Collection) {
|
|
|
- Collection<?> collection = (Collection<?>) values;
|
|
|
|
|
- return join(collection.toArray(new CharSequence[collection.size()]), separator);
|
|
|
|
|
|
|
+ return join( ((Collection<?>)values).toArray(new CharSequence[0]), separator );
|
|
|
}
|
|
}
|
|
|
StringBuilder builder = new StringBuilder();
|
|
StringBuilder builder = new StringBuilder();
|
|
|
boolean first = true;
|
|
boolean first = true;
|
|
@@ -115,8 +159,7 @@ public final class StringUtils {
|
|
|
|
|
|
|
|
public static <T extends CharSequence> String join(Iterable<T> values) {
|
|
public static <T extends CharSequence> String join(Iterable<T> values) {
|
|
|
if (values instanceof Collection) {
|
|
if (values instanceof Collection) {
|
|
|
- Collection<?> collection = (Collection<?>) values;
|
|
|
|
|
- return join(collection.toArray(new CharSequence[collection.size()]));
|
|
|
|
|
|
|
+ return join( ((Collection<?>)values).toArray(new CharSequence[0]) );
|
|
|
}
|
|
}
|
|
|
StringBuilder builder = new StringBuilder();
|
|
StringBuilder builder = new StringBuilder();
|
|
|
for(Object value : values) {
|
|
for(Object value : values) {
|
|
@@ -229,10 +272,34 @@ public final class StringUtils {
|
|
|
public static String clip(String text, int size) {
|
|
public static String clip(String text, int size) {
|
|
|
return text.substring(0, Math.min(size, text.length()));
|
|
return text.substring(0, Math.min(size, text.length()));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static String lpadd(String text, char padd, int size) {
|
|
|
|
|
+ int n = text.length();
|
|
|
|
|
+ int d = size - n;
|
|
|
|
|
+ if(d <= 0) {
|
|
|
|
|
+ return text;
|
|
|
|
|
+ }
|
|
|
|
|
+ char[] buffer = new char[size];
|
|
|
|
|
+ text.getChars(0, n, buffer, 0);
|
|
|
|
|
+ for(int i=n; i<size; i++) { buffer[i] = padd; }
|
|
|
|
|
+ return new String(buffer);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String rpadd(String text, char padd, int size) {
|
|
|
|
|
+ int n = text.length();
|
|
|
|
|
+ int d = size - n;
|
|
|
|
|
+ if(d <= 0) {
|
|
|
|
|
+ return text;
|
|
|
|
|
+ }
|
|
|
|
|
+ char[] buffer = new char[size];
|
|
|
|
|
+ text.getChars(0, n, buffer, d);
|
|
|
|
|
+ for(int i=0; i<d; i++) { buffer[i] = padd; }
|
|
|
|
|
+ return new String(buffer);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public static String lcommon(String value1, String value2) {
|
|
public static String lcommon(String value1, String value2) {
|
|
|
- if(null==value1 || value1.isEmpty() ) { return ""; }
|
|
|
|
|
- if(null==value2 || value2.isEmpty() ) { return ""; }
|
|
|
|
|
|
|
+ if( Strings.isEmpty(value1) ) { return ""; }
|
|
|
|
|
+ if( Strings.isEmpty(value2) ) { return ""; }
|
|
|
int n = Math.min(value1.length(), value2.length());
|
|
int n = Math.min(value1.length(), value2.length());
|
|
|
for(int i=0; i<n; i++) {
|
|
for(int i=0; i<n; i++) {
|
|
|
if( value1.charAt(i)!=value2.charAt(i) ) { return value1.substring(0,i); }
|
|
if( value1.charAt(i)!=value2.charAt(i) ) { return value1.substring(0,i); }
|
|
@@ -342,10 +409,27 @@ public final class StringUtils {
|
|
|
public CharSequence subSequence(int start, int end) {
|
|
public CharSequence subSequence(int start, int end) {
|
|
|
return new NCopies(text, start, end);
|
|
return new NCopies(text, start, end);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String toString() {
|
|
|
|
|
+ int n = length();
|
|
|
|
|
+ char[] data = new char[n];
|
|
|
|
|
+ for(int i=0; i<n; i++) { data[i] = charAt(i); }
|
|
|
|
|
+ return new String(data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static String escapeRegex(String text) {
|
|
public static String escapeRegex(String text) {
|
|
|
return REGEX_SPECIAL.matcher(text).replaceAll("\\\\$1");
|
|
return REGEX_SPECIAL.matcher(text).replaceAll("\\\\$1");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static boolean contains(String text, char value) {
|
|
|
|
|
+ return text.indexOf(value) != -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static boolean contains(String text, CharSequence value) {
|
|
|
|
|
+ return text.contains(value);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|