public class StringTool extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_EMAIL_LENGTH
According to RFC 3696
|
static int |
MAX_SIZE_IN_BYTES_FOR_ORACLE_VARCHAR2
Deprecated.
|
| Constructor and Description |
|---|
StringTool() |
| Modifier and Type | Method and Description |
|---|---|
static void |
addPathToVector(List<String> v,
String p)
Adds a path to the vector specified, if the path is an existing file
or directory, and it doesn't already exist in the vector.
|
static void |
addSearchEnvToVector(List<String> v,
String envvar)
Adds the value of a "search path environment variable" to the vector.
|
static void |
addSearchPathToVector(List<String> v,
String searchpath)
Takes a search path, i.e.
|
static void |
arrayToAsciiStr(Appendable sb,
byte[] ar,
int bi,
int nc)
Returns a string containing only printable chars for the given bytes.
|
static void |
arrayToDumpLine(Appendable sb,
byte[] ar,
int bi,
int nc)
Returns a dumpstring containing the offset, the hex bytes, and the ascii
representation of a given dump buffer.
|
static void |
arrayToHexStr(Appendable sb,
byte[] ar,
int bi,
int nc,
boolean fillout)
Returns a string of hex bytes for a given thing.
|
static void |
createInsertStatement(StringBuilder sb,
String table,
String pkname,
String pkexpr,
String[] fields) |
static void |
createUpdateStatement(StringBuilder sb,
String table,
String pkname,
String[] fields) |
static boolean |
dbGetBool(String fv)
Returns a boolean value from some database field.
|
static String |
dbSetBool(boolean v)
Returns a char(1) value to store in a database for booleans.
|
static byte[] |
decodeBase64(byte[] data)
This method decodes the given byte[] using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static byte[] |
decodeBase64(String str)
This method decodes the given string using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static String |
decodeBase64ToString(String str)
This method decodes the given string using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static String |
decodeURLEncoded(String encoded)
Decode the URLEncoded string passed to a real string.
|
static void |
dumpData(Appendable sb,
byte[] ar,
int off,
int len)
Dump the data as a formatted multiline buffer: like
|
static void |
dumpDebugLocation(org.slf4j.Logger log,
String msg) |
static void |
dumpErrorLocation(org.slf4j.Logger log,
String msg) |
static void |
dumpInfoLocation(org.slf4j.Logger log,
String msg) |
static void |
dumpLocation(String msg)
Kept in api since it was useful for some debugs while coding, use just for debug purposes only, do not use in produciton code.
|
static void |
dumpTraceLocation(org.slf4j.Logger log,
String msg) |
static void |
dumpWarnLocation(org.slf4j.Logger log,
String msg) |
static byte[] |
encodeBase64(byte[] data)
This method encodes the given byte[] using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static String |
encodeBase64(String str)
This method encodes the given string using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static String |
encodeBase64ToString(byte[] data) |
static void |
encodeURLEncoded(Appendable sb,
String str)
Encode the string passed to URLEncoded format.
|
static String |
encodeURLEncoded(String str) |
static void |
entitiesToUnicode(Appendable sb,
String str,
boolean ignoremarkers)
Scans the input string for entities and replaces all entities that
are actually found with their Unicode character code.
|
static int |
entityToUnicode(String ename)
Translates an entity name to unicode.
|
static boolean |
equalStringList(List<String> inl,
List<String> al,
boolean caseindependent) |
static String |
extractSingleLine(String in)
Tries to extract a single line of max.
|
static String |
fill(int count,
char character) |
static File |
findFileOnEnv(String pname,
String env)
Finds a filename along the classpath..
|
static File |
findFileOnPath(String fname,
String path)
Returns the complete filename of the first file that is found along the
path specified.
|
static String |
fixFileURL(String fileurl) |
static byte[] |
fromHex(String s)
Decodes a hex string into a byte array.
|
static String |
generateGUID()
Generate an unique identifier with reasonable expectations that it will be globally unique.
|
static byte[] |
getBase64Map() |
static List<String> |
getEnvironment()
Returns the list of environment variables of the supported OS's.
|
static String |
getExceptionMessage(Throwable t)
If the throwable passed as a message then return it verbatim, else
return the exception's classname.
|
static String |
getFileExtension(String fn)
Deprecated.
|
static String |
getFinalFrom(Class<?> cl,
long sval) |
static String |
getFinalFrom(Class<?> cl,
long sval,
String part)
Traverses a given class and tries to find a public
|
static String |
getFinalFrom(Class<?> cl,
long sval,
String part,
String ign)
Traverses a given class and tries to find a public
|
static int |
getJreVersion() |
static int |
getLevenshteinDistance(String s,
String t,
boolean ignorecase)
This returns the Levenshtein distance between two strings, which is the number of changes (adds, removes)
that are needed to convert source into target.
|
static String |
getLocation() |
static void |
getLocation(StringBuffer sb) |
static String |
getNextPathComponent(int ix,
String s,
boolean includeslash)
Find the 1st part of the path passed, i.e.
|
static String |
getNextPathComponent(String s,
boolean includeslash) |
static String |
getRandomStringWithPrefix(int length,
String prefix)
This methods creates a random String with the specified prefix and length given.
|
static String |
getString(InputStream is)
Deprecated.
|
static void |
htmlStringize(Appendable o,
String is)
Enter with a string; it returns the same string but replaces HTML
recognised characters with their &..; equivalent.
|
static String |
htmlStringize(String is)
Enter with a string; it returns the same string but replaces HTML
recognised characters with their &..; equivalent.
|
static void |
htmlStringize(StringBuilder sb,
String is)
Enter with a string; it returns the same string but replaces HTML
recognised characters with their &..; equivalent.
|
static void |
htmlStringizewithLF(StringBuilder sb,
String is)
Enter with a string; it returns the same string but replaces HTML
recognised characters with their &..; equivalent.
|
static String |
intToStr(int val,
int radix,
int npos)
Returns a number in the specified base, and with the specified #of
positions.
|
static boolean |
isAllSpaces(String s) |
static boolean |
isBlank(String s)
Checks if string is blank.
|
static boolean |
isDomainChar(char c) |
static boolean |
isEqual(Object a,
Object b) |
static boolean |
isEqualIgnoreCase(String a,
String b) |
static boolean |
isInteger(String string)
Checks if forwarded string can be parsed into an Integer.
|
static boolean |
isInvalidOracleLength(String text)
Checks whether a given text is too big for the maximum varchar2 database field
|
static boolean |
isNumber(String s)
Returns TRUE if the string is a number, possibly containing a '.'.
|
static boolean |
isValidDbFieldName(String s)
Field name must start with ascii letter, then letters, digits or _.
|
static boolean |
isValidDomainName(String s)
Checks if the name is a valid domain name.
|
static boolean |
isValidDottedName(String s) |
static boolean |
isValidEmail(String em) |
static boolean |
isValidJavaIdentifier(String s) |
static boolean |
isWhiteSpaceOrNbsp(char c) |
static void |
main(String[] args) |
static String |
makeSearchPath(List<String> v)
Returns a string buffer containing a search path variable from the
vector passed.
|
static String |
makeSearchPath(String[] v)
Returns a string buffer containing a search path variable from the
vector passed.
|
static void |
makeSearchPath(StringBuffer sb,
String[] v)
Returns a string buffer containing a search path variable from the
vector passed.
|
static void |
makeSearchPath(StringBuilder sb,
List<String> v)
Returns a string buffer containing a search path variable from the
vector passed.
|
static String |
makeURL(File f)
Workaround for Java bug delivering file:// instead of file:/// for
file.toURL().toString().
|
static String |
normalizeConcat(String current,
String tpl)
Called when generate() is called with a string.
|
static String |
normalizeUndot(String ins)
Takes an input URL and handles all '.' and '..' replacements.
|
static String |
normalizeURL(String current,
String tpl)
Called when generate() is called with a string.
|
static String |
oldStrOracleTruncate(String in,
int nchars)
Handles Oracle truncation rules:
If the string is > nchars truncate to nchars
Convert the string to bytes in UTF-8 encoding
If the string length, in bytes, is > 4000 bytes (the max stupid size of Oracle's stupid varchar2 column, stupid) remove characters until
the string fits the stupidly limited Oracle column
|
static String |
oldStrOracleTruncate(String in,
int nchars,
String suffixForTooLong)
Returns string that can fix into Oracle column, and in case that it has to be truncated it adds specified suffix to it, that is still
not exceding Oracle limit in total.
|
static void |
parseString(StringBuilder sb,
String s)
Takes a java string, without quotes, and replaces all escape sequences
in there with their actual character representation.
|
static void |
printHex(PrintStream pw,
byte[] arr) |
static void |
printHex(PrintStream pw,
byte[] arr,
int start,
int end) |
static void |
printHex(PrintWriter pw,
byte[] arr) |
static void |
printHex(PrintWriter pw,
byte[] arr,
int start,
int end) |
static String |
removeAccents(String str)
Replaces all accented letters with their non-accented equivalents - FOR WESTERN LANGUAGES ONLY!
|
static String |
removeRepeatingCharacters(String in)
Replaces long character sequences without space like ---- and ===== with
a way shorter version (single character).
|
static String |
renderAsRawHtml(String input,
boolean removeEndingNewLines)
Does some transformation of custom string to html output -> taking into account several customizations.
|
static String |
replaceNewLineChars(String content,
String replacement)
Replaces all end of line characters with space so that content is represented in one line.
|
static void |
strAddIntFixed(Appendable sb,
int val,
int radix,
int len)
Converts the integer to a string with a fixed length, adding leading zeroes
if needed.
|
static String |
strBackslashToSlash(String s)
Takes an input string and replaces all occurences of the backslash with
a forward slash.
|
static String |
strCapitalized(String name) |
static String |
strCommad(long val)
Returns a properly formatted commad string for a number [english only].
|
static String |
strDuration(long dlt) |
static String |
strDurationMillis(long dlt) |
static boolean |
strEndsWithIgnoreCase(String st,
String with)
Returns T if the string ends with the specified string, while ignoring
case.
|
static int |
strIndexOfIgnoreCase(String txt,
String match)
Tries to locate a substring in a string while ignoring case.
|
static StringBuffer |
stringize(String s)
Converts a string into a java-compilable version of a string, i.e.
|
static void |
stringize(StringBuffer sb,
String s) |
static StringBuffer |
stringizeNQ(String s) |
static String |
strNanoTime(long ns)
Return a nanotime timestamp with 2 thousands of precision max.
|
static String |
strOracleTruncate(String in,
int nchars) |
static String |
strOracleTruncate(String in,
int nchars,
String suffix) |
static String |
strReplace(String src,
String old,
String nw)
Case-sensitive replace of all occurrences of [old] with [new].
|
static String |
strSize(long sz)
Returns a string representing some size, in bytes.
|
static void |
strStacktrace(Appendable sb,
Throwable t) |
static String |
strStacktrace(Throwable t) |
static void |
strStacktraceFiltered(Appendable sb,
Throwable t,
String[] skipbefore,
String[] skipafter,
int linelimit)
Report a filtered location stack trace, where the start of the stack trace and the end can be removed.
|
static boolean |
strStartsWithIgnoreCase(String st,
String with)
Returns T if the string starts with the specified string, while ignoring
case.
|
static String |
strToFixedLength(String s,
char c,
int l)
Returns a string with the specified length.
|
static String |
strToFixedLength(String s,
int l)
Returns a string with the specified length.
|
static int |
strToInt(String v,
int defval) |
static void |
strToJavascriptString(Appendable w,
String cs,
boolean dblquote) |
static String |
strToJavascriptString(String cs,
boolean dblquote) |
static long |
strToLong(String v,
int six,
int eix,
int defval) |
static long |
strToLong(String v,
long defval) |
static String |
strTrunc(String s,
int len) |
static String |
strTruncateUtf8Bytes(String in,
int nchars,
int nbytes) |
static String |
strTruncateUtf8Bytes(String in,
int nchars,
int nbytes,
String suffix) |
static String |
strUnquote(String s) |
static String |
strUnspace(String s)
Removes all whitespace from a string.
|
static String |
toHex(byte[] arr)
Converts the byte array to a hex string.
|
static String |
toHex(byte[] arr,
int start,
int end)
Converts the byte array passed to a hex string.
|
static String |
toHexSp(byte[] arr)
Converts the byte array to a hex string.
|
static String |
toHexSp(byte[] arr,
int start,
int end)
Converts the byte array passed to a hex string.
|
static String |
toXY(int x,
int y)
Returns a coordinate pair as a string.
|
static String |
truncLeadingOracleColumn(String text,
int columnSize)
This method removes the leading characters from the string, if it exceeds the column size
or needs more then 4000 bytes to store in the database.
|
static String |
truncLength(String s,
int maxlen)
If the input string is too long, returns a substring containing at most
maxlen characters.
|
static void |
unicodeToEntities(StringBuffer sb,
String str)
Replaces all non-ascii stuff with their entities.
|
static String |
urlLastPart(String url)
Returns the last element (document name?) from the url passed.
|
static int |
utf8Length(String in) |
static int |
utf8Truncated(String in,
int length,
int maxbytes) |
static boolean |
validateUrl(String urlValue)
Loose checks if provided URL is valid.
It allows URLs with and without http, https, ftp...
|
static String |
xmlStringize(String is)
Enter with a string; it returns the same string but replaces HTML
recognised characters with their &..; equivalent.
|
static void |
xmlStringize(StringBuffer sb,
String is)
Enter with a string; it returns the same string but replaces HTML
recognised characters with their &..; equivalent.
|
static String |
xmlStringizeForDomApi(String is)
Converts input string to xml representation that complies to
DOM API 5.2 Character Escaping
http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
|
static void |
xmlStringizeForDomApi(StringBuffer sb,
String is)
Converts input string to xml representation that complies to
DOM API 5.2 Character Escaping
http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
|
@Deprecated public static final int MAX_SIZE_IN_BYTES_FOR_ORACLE_VARCHAR2
public static final int MAX_EMAIL_LENGTH
@Nonnull public static String getRandomStringWithPrefix(int length, @Nonnull String prefix)
length - prefix - public static boolean isValidDomainName(@Nonnull String s)
s - public static boolean isNumber(@Nonnull String s)
s - public static boolean isDomainChar(char c)
public static boolean isWhiteSpaceOrNbsp(char c)
public static boolean isValidDbFieldName(@Nonnull String s)
s - public static boolean isValidDottedName(String s)
public static final boolean isEqualIgnoreCase(@Nullable String a, @Nullable String b)
public static void stringize(@Nonnull StringBuffer sb, @Nonnull String s)
public static StringBuffer stringize(@Nonnull String s)
public static StringBuffer stringizeNQ(@Nonnull String s)
public static String strBackslashToSlash(@Nonnull String s)
public static String strSize(long sz)
public static void parseString(@Nonnull StringBuilder sb, @Nonnull String s)
public static String truncLength(@Nullable String s, int maxlen)
public static String strToFixedLength(String s, int l)
public static String strToFixedLength(@Nonnull String s, char c, int l)
public static int getLevenshteinDistance(@Nonnull String s, @Nonnull String t, boolean ignorecase)
public static boolean strStartsWithIgnoreCase(@Nonnull String st, @Nonnull String with)
st - the string whose start is to be checkedwith - the start stringpublic static boolean strEndsWithIgnoreCase(@Nonnull String st, @Nonnull String with)
st - the string whose end is to be checkedwith - the end stringpublic static int strIndexOfIgnoreCase(@Nonnull String txt, @Nonnull String match)
txt - match - @Nonnull public static String intToStr(int val, int radix, int npos)
public static void strAddIntFixed(@Nonnull Appendable sb, int val, int radix, int len)
sb - val - radix - len - @Nonnull public static String strCommad(long val)
val - public static void arrayToHexStr(@Nonnull Appendable sb, @Nonnull byte[] ar, int bi, int nc, boolean fillout) throws IOException
IOExceptionpublic static void arrayToAsciiStr(@Nonnull Appendable sb, @Nonnull byte[] ar, int bi, int nc) throws IOException
IOExceptionpublic static void arrayToDumpLine(@Nonnull Appendable sb, @Nonnull byte[] ar, int bi, int nc) throws IOException
IOExceptionpublic static void dumpData(@Nonnull Appendable sb, @Nonnull byte[] ar, int off, int len) throws IOException
0000 ff ef aa bb cc dd 99 88 ff ef aa bb cc dd 99 88 sgdfkajse
sb - ar - off - len - IOExceptionpublic static void printHex(@Nonnull PrintWriter pw, @Nonnull byte[] arr)
public static void printHex(@Nonnull PrintWriter pw, @Nonnull byte[] arr, int start, int end)
public static void printHex(@Nonnull PrintStream pw, @Nonnull byte[] arr)
public static void printHex(@Nonnull PrintStream pw, @Nonnull byte[] arr, int start, int end)
public static String toHex(byte[] arr, int start, int end)
arr - the array containing the data to convertstart - the first byte in the array to convertend - the exclusive end of the region to convertpublic static String toHex(byte[] arr)
arr - public static String toHexSp(byte[] arr, int start, int end)
arr - the array containing the data to convertstart - the first byte in the array to convertend - the exclusive end of the region to convertpublic static String toHexSp(byte[] arr)
arr - public static byte[] fromHex(String s) throws Exception
s - the stringException - if the array is malformed.public static File findFileOnPath(String fname, String path)
public static File findFileOnEnv(String pname, String env)
@Deprecated public static String getFileExtension(String fn)
FileTool.getFileExtension(String)public static void addPathToVector(List<String> v, String p)
public static void addSearchPathToVector(List<String> v, String searchpath)
public static void addSearchEnvToVector(List<String> v, String envvar)
addSearchPathToVector()public static void makeSearchPath(StringBuilder sb, List<String> v)
public static String makeSearchPath(List<String> v)
public static void makeSearchPath(StringBuffer sb, String[] v)
public static String makeSearchPath(String[] v)
public static String getFinalFrom(Class<?> cl, long sval, String part)
public static String getFinalFrom(Class<?> cl, long sval, String part, String ign)
public static String urlLastPart(String url)
public static String htmlStringize(String is)
public static void htmlStringize(StringBuilder sb, String is)
public static void htmlStringizewithLF(StringBuilder sb, String is)
public static void htmlStringize(Appendable o, String is) throws Exception
Exceptionpublic static String xmlStringize(String is)
public static String xmlStringizeForDomApi(String is)
public static void xmlStringize(StringBuffer sb, String is)
public static void xmlStringizeForDomApi(StringBuffer sb, String is)
public static void entitiesToUnicode(Appendable sb, String str, boolean ignoremarkers) throws IOException
sb - the buffer to append the string tostr - the string to copy while replacing entities.IOExceptionpublic static void unicodeToEntities(StringBuffer sb, String str)
sb - str - public static int entityToUnicode(String ename)
ename - public static void strToJavascriptString(Appendable w, String cs, boolean dblquote)
public static boolean dbGetBool(String fv)
public static String dbSetBool(boolean v)
public static void main(String[] args)
public static List<String> getEnvironment()
public static final byte[] getBase64Map()
public static final String encodeBase64(String str)
str - the stringpublic static final byte[] encodeBase64(byte[] data)
data - the datapublic static final String decodeBase64ToString(String str)
str - the base64-encoded string.public static final byte[] decodeBase64(String str)
str - the base64-encoded string.public static final byte[] decodeBase64(byte[] data)
data - the base64-encoded data.public static final String encodeBase64ToString(byte[] data)
public static void strStacktrace(Appendable sb, Throwable t)
public static void strStacktraceFiltered(Appendable sb, Throwable t, String[] skipbefore, String[] skipafter, int linelimit)
sb - t - skipbefore - skipafter - public static final String normalizeURL(String current, String tpl) throws Exception
Called when generate() is called with a string. This must decode the string into a key object that can be used by the decodeInputURL key to determine a resource provider and a provider-relative key.
This default implementation assumes that the key is to be a normal URL string, and uses path semantics to create the actual key from the string passed: if it contains a host name it is stripped; if it is relative then the complete path is appended.
Exceptionpublic static final String normalizeConcat(String current, String tpl) throws Exception
Called when generate() is called with a string. This must decode the string into a key object that can be used by the decodeInputURL key to determine a resource provider and a provider-relative key.
This default implementation assumes that the key is to be a normal URL string, and uses path semantics to create the actual key from the string passed: if it contains a host name it is stripped; if it is relative then the complete path is appended.
Exceptionpublic static final String normalizeUndot(String ins)
ins - the input URLpublic static int strToInt(String v, int defval)
public static long strToLong(String v, long defval)
public static long strToLong(String v, int six, int eix, int defval)
public static String getNextPathComponent(int ix, String s, boolean includeslash)
s - public static boolean equalStringList(List<String> inl, List<String> al, boolean caseindependent)
public static String makeURL(File f)
f - public static void encodeURLEncoded(Appendable sb, String str)
sb - data - public static String decodeURLEncoded(String encoded)
encoded - public static final String getLocation()
public static final void getLocation(StringBuffer sb)
public static final void dumpLocation(String msg)
msg - public static final void dumpDebugLocation(@Nonnull org.slf4j.Logger log, @Nonnull String msg)
public static final void dumpTraceLocation(@Nonnull org.slf4j.Logger log, @Nonnull String msg)
public static final void dumpInfoLocation(@Nonnull org.slf4j.Logger log, @Nonnull String msg)
public static final void dumpWarnLocation(@Nonnull org.slf4j.Logger log, @Nonnull String msg)
public static final void dumpErrorLocation(@Nonnull org.slf4j.Logger log, @Nonnull String msg)
public static String strUnspace(String s)
s - public static String oldStrOracleTruncate(String in, int nchars)
in - nchars - @Nullable public static String strTruncateUtf8Bytes(@Nullable String in, int nchars, int nbytes)
public static int utf8Truncated(String in, int length, int maxbytes)
public static int utf8Length(String in)
public static String strTruncateUtf8Bytes(String in, int nchars, int nbytes, String suffix)
public static String oldStrOracleTruncate(String in, int nchars, String suffixForTooLong)
in - nchars - suffixForTooLong - public static String strNanoTime(long ns)
ns - public static String strReplace(String src, String old, String nw)
src - old - nw - public static String getExceptionMessage(Throwable t)
t - public static int getJreVersion()
public static String removeRepeatingCharacters(String in)
in - maxlen - public static String extractSingleLine(String in)
in - @Nonnull public static String generateGUID()
public static boolean isBlank(String s)
s - String to be validated.public static void createInsertStatement(StringBuilder sb, String table, String pkname, String pkexpr, String[] fields)
public static void createUpdateStatement(StringBuilder sb, String table, String pkname, String[] fields)
public static String fill(int count, char character)
@Nonnull public static String removeAccents(@Nonnull String str)
str - @Deprecated @Nonnull public static String getString(@Nonnull InputStream is) throws Exception
FileTool.readStreamAsString(InputStream, String) instead. It can be parametrized by "UTF-8" we use here.is - Exception@Nullable public static String truncLeadingOracleColumn(@Nullable String text, int columnSize)
This method removes the leading characters from the string, if it exceeds the column size or needs more then 4000 bytes to store in the database. There's a limit on the size in bytes for an Oracle varchar2. This limit is 4000 bytes (MAX_SIZE_IN_BYTES_FOR_ORACLE_VARCHAR2). So if you declare a varchar2(4000 char), it's possible that for example 3970 characters won't fit, because there are charaters in the string that needs more then one byte in UTF8.
text - public static boolean isInvalidOracleLength(@Nonnull String text)
text - public static boolean isInteger(@Nullable String string)
string - @Nonnull public static String replaceNewLineChars(@Nonnull String content, @Nonnull String replacement)
content - that should be without new line charactersreplacement - for new line characterspublic static boolean validateUrl(@Nullable String urlValue)
urlValue - public static String renderAsRawHtml(@Nonnull String input, boolean removeEndingNewLines)
input - Copyright © 2017 etc.to. All rights reserved.