📄 stringutil.java
字号:
package jodd.util;
import jodd.bean.ConvertersManager;
/**
* General string utils.
*/
public final class StringUtil {
// ---------------------------------------------------------------- replace
/**
* Replaces the occurences of a certain pattern in a string with a
* replacement String. This is the fastest replace function known to author.
*
* @param s the string to be inspected
* @param sub the string pattern to be replaced
* @param with the string that should go where the pattern was
*
* @return the string with the replacements done
*/
public static String replace(String s, String sub, String with) {
if ((s == null) || (sub == null) || (with == null)) {
return s;
}
int c = 0;
int i = s.indexOf(sub, c);
if (i == -1) {
return s;
}
StringBuffer buf = new StringBuffer(s.length() + with.length());
do {
buf.append(s.substring(c,i));
buf.append(with);
c = i + sub.length();
} while ((i = s.indexOf(sub, c)) != -1);
if (c < s.length()) {
buf.append(s.substring(c,s.length()));
}
return buf.toString();
}
/**
* Character replacement in a string. All occurrencies of a character will be
* replaces.
*
* @param s input string
* @param sub character to replace
* @param with character to replace with
*
* @return string with replaced characters
*/
public static String replace(String s, char sub, char with) {
if (s == null) {
return s;
}
char[] str = s.toCharArray();
for (int i = 0; i < str.length; i++) {
if (str[i] == sub) {
str[i] = with;
}
}
return new String(str);
}
/**
* Replaces the very first occurance of a substring with suplied string.
*
* @param s source string
* @param sub substring to replace
* @param with substring to replace with
*
* @return modified source string
*/
public static String replaceFirst(String s, String sub, String with) {
if ((s == null) || (sub == null) || (with == null)) {
return s;
}
int i = s.indexOf(sub);
if (i == -1) {
return s;
}
return s.substring(0, i) + with + s.substring(i + sub.length());
}
/**
* Replaces the very first occurence of a character in a String.
*
* @param s string
* @param sub char to replace
* @param with char to replace with
*
* @return modified string
*/
public static String replaceFirst(String s, char sub, char with) {
if (s == null) {
return s;
}
char[] str = s.toCharArray();
for (int i = 0; i < str.length; i++) {
if (str[i] == sub) {
str[i] = with;
break;
}
}
return new String(str);
}
/**
* Replaces the very last occurance of a substring with suplied string.
*
* @param s source string
* @param sub substring to replace
* @param with substring to replace with
*
* @return modified source string
*/
public static String replaceLast(String s, String sub, String with) {
if ((s == null) || (sub == null) || (with == null)) {
return s;
}
int i = s.lastIndexOf(sub);
if (i == -1) {
return s;
}
return s.substring(0, i) + with + s.substring(i + sub.length());
}
/**
* Replaces the very last occurence of a character in a String.
*
* @param s string
* @param sub char to replace
* @param with char to replace with
*
* @return modified string
*/
public static String replaceLast(String s, char sub, char with) {
if (s == null) {
return s;
}
char[] str = s.toCharArray();
for (int i = str.length - 1; i >= 0; i--) {
if (str[i] == sub) {
str[i] = with;
break;
}
}
return new String(str);
}
// ---------------------------------------------------------------- misc
/**
* Compares 2 strings. If one of the strings is null, false is returned. if
* both string are null, true is returned.
*
* @param s1 first string to compare
* @param s2 second string
*
* @return true if strings are equal, otherwise false
*/
public static boolean equals(String s1, String s2) {
return Util.equals(s1, s2);
}
/**
* Determines if a string is empty. If string is NULL, it returns true.
*
* @param s string
*
* @return true if string is empty or null.
*/
public static boolean isEmpty(String s) {
if (s != null) {
return (s.length() == 0);
}
return true;
}
/**
* Set the maximum length of the string. If string is longer, it will be
* shorten.
*
* @param s string
* @param len max number of characters in string
*
* @return string with length no more then specified
*/
public static String setMaxLength(String s, int len) {
if (s == null) {
return s;
}
if (s.length() > len) {
s = s.substring(0, len);
}
return s;
}
/**
* Converts an object to a String. If object is <code>null</code> it will be
* not converted.
*
* @param obj object to convert to string
*
* @return string created from the object or <code>null</code>
*/
public static String toString(Object obj) {
if (obj == null) {
return (String)null;
}
return obj.toString();
}
/**
* Converts an object to a String. If object is <code>null</code> a empty
* string is returned.
*
* @param obj object to convert to string
*
* @return string created from the object
*/
public static String toNotNullString(Object obj) {
if (obj == null) {
return "";
}
return obj.toString();
}
/**
* Converts an object to a String Array.
*
* @param obj object to convert to string array
*
* @return string array created from the object
*/
public static String[] toStringArray(Object obj) {
return (String[]) ConvertersManager.convert(obj, String[].class);
}
// ---------------------------------------------------------------- split
/**
* Splits a string in several parts (tokens) that are separated by delimeter.
* Delimeter is <b>always</b> surrounded by two strings! If there is no
* content between two delimeters, empty string will be returned for that
* token. Therefore, the length of the returned array will always be:
* #delimeters + 1.<br><br>
*
* Method is much, much faster then regexp <code>String.split()</code>,
* and a bit faster then <code>StringTokenizer</code>.
*
* @param src string to split
* @param delimeter split delimeter
*
* @return array of splitted strings
*/
public static String[] split(String src, String delimeter) {
if (src == null) {
return null;
}
if (delimeter == null) {
return new String[] {src};
}
int maxparts = (src.length() / delimeter.length()) + 2; // one more for the last
int[] positions = new int[maxparts];
int dellen = delimeter.length();
int i = 0, j = 0;
int count = 0;
positions[0] = - dellen;
while ((i = src.indexOf(delimeter, j)) != -1) {
count++;
positions[count] = i;
j = i + dellen;
}
count++;
positions[count] = src.length();
String[] result = new String[count];
for (i = 0; i < count; i++) {
result[i] = src.substring(positions[i] + dellen, positions[i + 1]);
}
return result;
}
/**
* Splits a string in several parts (tokens) that are separated by deliemter
* characters. Deliemter may contains any number of character, and it is
* always surrounded by two strings.
*
* @param src source to examine
* @param d string with delimeter characters
*
* @return array of tokens
*/
public static String[] splitc(String src, String d) {
if (src == null) {
return null;
}
if ((d == null) || (d.length() == 0) || (src.length() == 0) ) {
return new String[] {src};
}
char[] delimeters = d.toCharArray();
char[] srcc = src.toCharArray();
int maxparts = srcc.length + 1;
int[] start = new int[maxparts];
int[] end = new int[maxparts];
int count = 0;
start[0] = 0;
int s = 0, e;
if (CharUtil.equals(srcc[0], delimeters) == true) { // string starts with delimeter
end[0] = 0;
count++;
s = CharUtil.findFirstDiff(srcc, 1, delimeters);
if (s == -1) { // nothing after delimeters
return new String[] {"", ""};
}
start[1] = s; // new start
}
while (true) {
// find new end
e = CharUtil.findFirstAny(srcc, s, delimeters);
if (e == -1) {
end[count] = srcc.length;
break;
}
end[count] = e;
// find new start
count++;
s = CharUtil.findFirstDiff(srcc, e, delimeters);
if (s == -1) {
start[count] = end[count] = srcc.length;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -