import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class StringUtil {
private StringUtil() {
// Utility class, hide the constructor.
}
// Validators ---------------------------------------------------------------------------------
/**
* Check if given string is a number. It should contain digits only.
* @param string The string to check on.
* @return True if string is a number.
*/
public static boolean isNumber(String string) {
return string.matches("^\\d+$");
}
/**
* Check if given string is numeric. Positive and negative prefix and dot separators are
* allowed.
* @param string The string to check on.
* @return True if string is numeric.
*/
public static boolean isNumeric(String string) {
return string.matches("^[-+]?\\d+(\\.\\d+)?$");
}
/**
* Check if given string is a value. The dot separator and two decimals are required.
* @param string The string to check on.
* @return True if string is value.
*/
public static boolean isValue(String string) {
return string.matches("^\\d+\\.\\d{2}$");
}
/**
* Check if given string contains numbers.
* @param string The string to check on.
* @return True if string contains numbers.
*/
public static boolean hasNumbers(String string) {
return string.matches("^.*\\d.*$");
}
/**
* Check if given string is a valid email address.
* @param string The string to check on.
* @return True if string is an valid email address.
*/
public static boolean isEmailAddress(String string) {
return string.matches("^[\\w-~#&]+(\\.[\\w-~#&]+)*@([\\w-]+\\.)+[a-z]{2,5}$");
}
// Converters ---------------------------------------------------------------------------------
/**
* Convert the given string to a string with unicode escape sequence for every character.
* @param string The string to be converted.
* @return The string with unicode escape sequence for every character.
*/
public static String toUnicode(String string) {
StringBuilder builder = new StringBuilder();
for (char c : string.toCharArray()) {
builder.append(String.format("\\u%04x", (int) c));
}
return builder.toString();
}
/**
* Unescape any unicode escape sequence in the given string.
* @param string The string to be unescaped.
* @return The string with unescaped unicode escape sequences.
*/
public static String unescapeUnicode(String string) {
Matcher matcher = Pattern.compile("\\\\u((?i)[0-9a-f]{4})").matcher(string);
while (matcher.find()) {
int codepoint = Integer.valueOf(matcher.group(1), 16);
string = string.replaceAll("\\" + matcher.group(0), String.valueOf((char) codepoint));
}
return string;
}
/**
* Escape characters for text appearing as XML data, between tags.
*/
public static String forXML(String aText){
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(aText);
char character = iterator.current();
while (character != CharacterIterator.DONE ){
if (character == '<') {
result.append("<");
}
else if (character == '>') {
result.append(">");
}
else if (character == '\"') {
result.append(""");
}
else if (character == '\'') {
result.append("'");
}
else if (character == '&') {
result.append("&");
}
else {
//the char is not a special one
//add it to the result as is
result.append(character);
}
character = iterator.next();
}
return result.toString();
}
/**
* Remove any XSS (Cross Site Scripting) vulnerabilities from the given string.
* @param string The string to remove XSS from.
* @return The string with removed XSS, if any.
*/
public static String removeXss(String string) {
return string
.replaceAll("(?i)