add utility method to check whether a string is valid hexadecimal number

This commit is contained in:
marhali 2022-02-22 21:45:44 +01:00
parent 68ec11a4e9
commit 85073f7f5b

View File

@ -3,6 +3,7 @@ package de.marhali.easyi18n.util;
import org.jetbrains.annotations.NotNull;
import java.io.StringWriter;
import java.util.regex.Pattern;
/**
* String utilities
@ -10,6 +11,17 @@ import java.io.StringWriter;
*/
public class StringUtil {
/**
* Checks if the provided String represents a hexadecimal number.
* For example: {@code 0x100...}, {@code -0x100...} and {@code +0x100...}.
* @param string String to evaluate
* @return true if hexadecimal string otherwise false
*/
public static boolean isHexString(@NotNull String string) {
final Pattern hexNumberPattern = Pattern.compile("[+-]?0[xX][0-9a-fA-F]+");
return hexNumberPattern.matcher(string).matches();
}
/**
* Escapes control characters for the given input string.
* Inspired by Apache Commons (see {@link org.apache.commons.lang.StringEscapeUtils}