// Copyright (c) 2003-2012, Jodd Team (jodd.org). All Rights Reserved.
package jodd.util;
import jodd.typeconverter.Convert;
import static jodd.util.StringPool.EMPTY;
import java.io.UnsupportedEncodingException;
/**
* Various String utilities.
* @see jodd.util.TextUtil
*/
public class StringUtil {
// ---------------------------------------------------------------- replace
/**
* Replaces all occurrences of a certain pattern in a string with a
* replacement string. This is the fastest replace function known to author.
*
* @param s string to be inspected
* @param sub string pattern to be replaced
* @param with string that should go where the pattern was
*/
public static String replace(String s, String sub, String with) {
int c = 0;
int i = s.indexOf(sub, c);
if (i == -1) {
return s;
}
int length = s.length();
StringBuilder sb = new StringBuilder(length + with.length());
do {
sb.append(s.substring(c, i));
sb.append(with);
c = i + sub.length();
} while ((i = s.indexOf(sub, c)) != -1);
if (c < length) {
sb.append(s.substring(c, length));
}
return sb.toString();
}
/**
* Replaces all occurrences of a character in a string.
*
* @param s input string
* @param sub character to replace
* @param with character to replace with
*/
public static String replaceChar(String s, char sub, char with) {
int startIndex = s.indexOf(sub);
if (startIndex == -1) {
return s;
}
char[] str = s.toCharArray();
for (int i = startIndex; i < str.length; i++) {
if (str[i] == sub) {
str[i] = with;
}
}
return new String(str);
}
/**
* Replaces all occurrences of a characters in a string.
*
* @param s input string
* @param sub characters to replace
* @param with characters to replace with
*/
public static String replaceChars(String s, char[] sub, char[] with) {
char[] str = s.toCharArray();
for (int i = 0; i < str.length; i++) {
char c = str[i];
for (int j = 0; j < sub.length; j++) {
if (c == sub[j]) {
str[i] = with[j];
break;
}
}
}
return new String(str);
}
/**
* Replaces the very first occurrence of a substring with supplied string.
*
* @param s source string
* @param sub substring to replace
* @param with substring to replace with
*/
public static String replaceFirst(String s, String sub, String with) {
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 occurrence of a character in a string.
*
* @param s string
* @param sub char to replace
* @param with char to replace with
*/
public static String replaceFirst(String s, char sub, char with) {
int index = s.indexOf(sub);
if (index == -1) {
return s;
}
char[] str = s.toCharArray();
str[index] = with;
return new String(str);
}
/**
* Replaces the very last occurrence of a substring with supplied string.
*
* @param s source string
* @param sub substring to replace
* @param with substring to replace with
*/
public static String replaceLast(String s, String sub, String with) {
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 occurrence of a character in a string.
*
* @param s string
* @param sub char to replace
* @param with char to replace with
*/
public static String replaceLast(String s, char sub, char with) {
int index = s.lastIndexOf(sub);
if (index == -1) {
return s;
}
char[] str = s.toCharArray();
str[index] = with;
return new String(str);
}
// ---------------------------------------------------------------- remove
/**
* Removes all substring occurrences from the string.
*
* @param s source string
* @param sub substring to remove
*/
public static String remove(String s, String sub) {
int c = 0;
int sublen = sub.length();
if (sublen == 0) {
return s;
}
int i = s.indexOf(sub, c);
if (i == -1) {
return s;
}
StringBuilder sb = new StringBuilder(s.length());
do {
sb.append(s.substring(c, i));
c = i + sublen;
} while ((i = s.indexOf(sub, c)) != -1);
if (c < s.length()) {
sb.append(s.substring(c, s.length()));
}
return sb.toString();
}
/**
* Removes all characters contained in provided string.
*
* @param src source string
* @param chars string containing characters to remove
*/
public static String removeChars(String src, String chars) {
int i = src.length();
StringBuilder sb = new StringBuilder(i);
for (int j = 0; j < i; j++) {
char c = src.charAt(j);
if (chars.indexOf(c) == -1) {
sb.append(c);
}
}
return sb.toString();
}
/**
* Removes set of characters from string.
*
* @param src string
* @param chars characters to remove
*/
public static String removeChars(String src, char... chars) {
int i = src.length();
StringBuilder sb = new StringBuilder(i);
mainloop:
for (int j = 0; j < i; j++) {
char c = src.charAt(j);
for (char aChar : chars) {
if (c == aChar) {
continue mainloop;
}
}
sb.append(c);
}
return sb.toString();
}
/**
* Removes a single character from string.
*
* @param string source string
* @param ch character to remove
*/
public static String remove(String string, char ch) {
int stringLen = string.length();
char[] result = new char[stringLen];
int offset = 0;
for (int i = 0; i < stringLen; i++) {
char c = string.charAt(i);
if (c == ch) {
continue;
}
result[offset] = c;
offset++;
}
return new String(result, 0, offset);
}
// ---------------------------------------------------------------- miscellaneous
/**
* Compares 2 strings. If one of the strings is <code>null</code>, <code>false</code> is returned. if
* both string are <code>null</code>, <code>true</code> is returned.
*
* @param s1 first string to compare
* @param s2 second string
*
* @return <code>true</code> if strings are equal, otherwise <code>false</code>
*/
public static boolean equals(String s1, String s2) {
return ObjectUtil.equals(s1, s2);
}
/**
* Determines if a string is empty (<code>null</code> or zero-length).
*/
public static boolean isEmpty(String string) {
return ((string == null) || (string.length() == 0));
}
/**
* Determines if string array contains empty strings.
* @see #isEmpty(String)
*/
public static boolean isAllEmpty(String... strings) {
for (String string : strings) {
if (isEmpty(string) == false) {
return false;
}
}
return true;
}
/**
* Determines if a string is blank (<code>null</code> or {@link #containsOnlyWhitespaces(String)}).
*/
public static boolean isBlank(String string) {
return ((string == null) || containsOnlyWhitespaces(string));
}
/**
* Determines if string is not blank.
*/
public static boolean isNotBlank(String string) {
return ((string != null) && !containsOnlyWhitespaces(string));
}
/**
* Determines if string array contains just blank strings.
*/
public static boolean isAllBlank(String... strings) {
for (String string : strings) {
if (isBlank(string) == false) {
return false;
}
}
return true;
}
/**
* Returns <code>true</code> if string contains only white spaces.
*/
public static boolean containsOnlyWhitespaces(String string) {
int size = string.length();
for (int i = 0; i < size; i++) {
char c = string.charAt(i);
if (CharUtil.isWhitespace(c) == fal