public final class StringHelper extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
maxLogCollectionSize
Limit size of collection to be logged.
If an object is logged and it is a collection and the size exceeds maxLogCollectionSize, only the number
of items is logged in objectToLoggableString(java.lang.Object)
instead of each item. |
static Function<String,String> |
stringNormalizer
The default normalizer used within the application.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
blank(char[] buf)
Null-safe char array filler.
Fills with blanks. |
static int |
countOccurrences(String str,
char c)
Counts the number of occurrences of a given character in a string.
|
static String |
digitToString(Character digit)
Gets the word-string for a digit.
Example: |
static boolean |
equalsIgnoreCase(String a,
String b)
Null safe
String.equalsIgnoreCase(String). |
static void |
fill(char[] buf,
char val)
Null-safe char array filler.
|
static String |
fillLeft(String str,
int length,
char filler)
Fills up a string from the left with a filler character to match a given length.
If the string is already longer, nothing happens. |
static String |
fillRight(String str,
int length,
char filler)
Fills up a string to the right with a filler character to match a given length.
If the string is already longer, nothing happens. |
static String |
firstBefore(String str,
char delimiter)
Gets the part of a string before a given delimiter.
|
static String |
firstLine(String str)
Gets the first line from a multi-line string.
|
static String |
firstToLower(String str)
Converts the first character of string to uppercase.
|
static String |
firstToUpper(String str)
Converts the first character of string to uppercase.
|
static String |
getContinuedLine(String line)
Checks whether given string introduces a continuation line.
|
static String |
getPlainClassName(String className)
Gets a classname without any optional generics.
|
static String |
hash(String algorithm,
char[] salt,
char[] input)
Builds a hash from an array of chars.
Note that this method converts the characters to bytes via toBytes(char[])
before applying the hash. |
static boolean |
isAllDigits(String str)
checks if a string contains only digits, i.e.
|
static boolean |
isAllDigits(String str,
boolean whitespaceAllowed)
Checks if a string contains only digits or whitespaces, i.e.
|
static boolean |
isAllWhitespace(String str)
Checks if string contains only whitespaces.
|
static boolean |
isReservedWord(String word)
Returns whether given string is a reserved java keyword.
|
static boolean |
isValidJavaClassName(String className)
Checks that given string is a valid Java classname.
Both classnames with a full classpath or without are validated. |
static boolean |
isValidJavaIdentifier(String str)
Returns whether the given string is a valid java identifier.
|
static boolean |
isValidJavaPackageName(String packageName)
Checks that given string is a valid Java packagename.
This is very restrictive verification. |
static String |
lastAfter(String str,
char delimiter)
Gets the part of a string after a given delimiter.
|
static String |
normalize(String str)
Normalizes a string (phonetically) for use as PDO.normText
using
stringNormalizer. |
static String |
normalize(String str,
Function<String,String> normalizer)
Normalizes a string (phonetically) for use as PDO.normText.
|
static String |
objectArrayToString(Object[] objArray,
String separator)
Creates a string from an object array.
|
static String |
objectToLoggableString(Object object)
Converts a value to a loggable string.
|
static String |
parseString(String str)
Parses a string.
The string may be enclosed in double- or single quotes which will be removed. |
static String |
readTextFromResource(String resourceName)
Reads a textfile from a resource.
The textfile must be in the classpath. |
static String |
removeTrailingText(String str,
String trail)
Removes a trailing text from a string.
|
static List<String> |
split(String str,
String delimiters)
Splits a string keeping strings together.
The strings may be enclosed in double- or single quotes which will not be removed. |
static String |
stripEnclosingDoubleQuotes(String str)
Strips enclosing double quotes.
|
static String |
toAsciiLetterOrDigit(String str)
Converts to string containing only the letters A-z or digits.
All other characters will be converted to an underscore. |
static byte[] |
toBytes(char c)
Converts a single (unicode) char to a byte-array.
|
static byte[] |
toBytes(char[] chars)
Converts a char-array to a byte-array.
|
static char[] |
toCharArray(String str)
Transforms a string to a char-array.
|
static String |
toDoubleQuotes(String str)
Takes a string, surrounds it with double-quotes and escapes all double-quotes
already in the string according to Unix rules.
|
static String |
toDTA(String str)
Filters illegal chars for DTA-Files (German "DATENAUSTAUSCH" banking format).
|
static String |
toFixedLength(String str,
int length)
Takes a string and returns one with a given length, cutting or
filling up with spaces, whatever appropriate.
|
static String |
toFixedLength(String str,
int length,
char filler)
Takes a string and returns one with a given length, cutting or
filling up with fillchars, whatever appropriate.
|
static String |
toFixedLengthLeftFill(String str,
int length)
Takes a string and returns one with a given length, cutting or
filling up with spaces from the left, whatever appropriate.
|
static String |
toFixedLengthLeftFill(String str,
int length,
char filler)
Takes a string and returns one with a given length, cutting or
filling up with fillchars from the left, whatever appropriate.
|
static String |
toHexString(byte[] binaryData)
Creates a human-readable hex-String out of a byte-array (e.g.
|
static String |
toHTML(String text)
Converts a multiline string to an HTML-string that
can be displayed in a label.
|
static String |
toLower(String str)
Converts a string to lowercase allowing null values.
|
static String |
toParsableString(String str)
Converts a string to a string parsable by
parseString(java.lang.String). |
static String |
toUpper(String str)
Converts a string to uppercase allowing null values.
|
static String |
toVarName(String str)
Filters illegal chars for Java variable names.
|
static String |
trim(String s)
Trims a string.
|
static String |
trim(String s,
int max)
Trims a string.
|
static String |
trimLeft(String str,
char filler)
Cuts leading characters.
|
static String |
trimRight(String str,
char filler)
Cuts trailing characters.
|
static String |
unDiacrit(String str,
boolean keepLength)
Converts special unicode characters (so-called diacrits) to standard ascii.
Supports also special german and northern european "umlauts". |
public static int maxLogCollectionSize
maxLogCollectionSize, only the number
of items is logged in objectToLoggableString(java.lang.Object)
instead of each item.public static boolean equalsIgnoreCase(String a, String b)
String.equalsIgnoreCase(String).a - first string, may be nullb - second string, may be nullpublic static String toUpper(String str)
str - the stringpublic static String toLower(String str)
str - the stringpublic static String firstToUpper(String str)
str - the stringpublic static String firstToLower(String str)
str - the stringpublic static String getPlainClassName(String className)
className - the original classnamepublic static String getContinuedLine(String line)
This is the case if the last character is an unquoted backslash.
line - the source linepublic static String toDoubleQuotes(String str)
Length 5" --> "Length 5\""
str - the stringpublic static String parseString(String str)
\n = new line \r = carriage return \t = tab \f = form feed
str - the source stringpublic static String toParsableString(String str)
parseString(java.lang.String).str - the stringpublic static List<String> split(String str, String delimiters)
\n = new line \r = carriage return \t = tab \f = form feed
str - the stringdelimiters - the delimiter characterspublic static boolean isValidJavaClassName(String className)
className - the classnamepublic static boolean isValidJavaPackageName(String packageName)
packageName - the packagenamepublic static boolean isValidJavaIdentifier(String str)
str - the stringpublic static String readTextFromResource(String resourceName) throws IOException
Example:
String model = StringHelper.readTestFromResource("org/tentackle/model/TestModel.txt");
resourceName - the name of the resourceIOException - if loading failed or no such resourcepublic static boolean isAllWhitespace(String str)
str - the string to checkpublic static boolean isReservedWord(String word)
word - the string to testpublic static String digitToString(Character digit)
'0' --> "zero"
digit - the digit characterpublic static String unDiacrit(String str, boolean keepLength)
str - the string to be convertedkeepLength - true if the length should be kept, i.e. no Ä to AE, but to Apublic static String normalize(String str, Function<String,String> normalizer)
str - the string to be normalizednormalizer - the normalizer to use, null = global defaultpublic static String normalize(String str)
stringNormalizer.str - the string to be normalizedpublic static String stripEnclosingDoubleQuotes(String str)
str - the original stringpublic static String toFixedLength(String str, int length, char filler)
str - the stringlength - the length of the returned stringfiller - the character to fill up if str is too shortpublic static String toFixedLength(String str, int length)
str - the stringlength - the length of the returned stringpublic static String toFixedLengthLeftFill(String str, int length, char filler)
str - the stringlength - the length of the returned stringfiller - the character to fill up if str is too shortpublic static String toFixedLengthLeftFill(String str, int length)
str - the stringlength - the length of the returned stringpublic static String toDTA(String str)
str - the stringpublic static String toVarName(String str)
str - the stringpublic static boolean isAllDigits(String str, boolean whitespaceAllowed)
str - the string to checkwhitespaceAllowed - true if whitespaces are allowedpublic static boolean isAllDigits(String str)
str - the string to checkpublic static String trim(String s, int max)
s - the string, may be nullmax - the maximum number of characters, 0 = minimum lengthpublic static String trim(String s)
s - the string, may be nulltrim(java.lang.String, int)public static String trimRight(String str, char filler)
Removes all trailing characters of given value from the string. If the string consists of those characters only, the returned string will empty.
str - the stringfiller - the filler to removepublic static String trimLeft(String str, char filler)
Removes all leading characters of given value from the string. If the string consists of those characters only, the returned string will be empty.
str - the stringfiller - the filler to removepublic static String fillLeft(String str, int length, char filler)
str - the stringlength - the desired lengthfiller - the filler characterpublic static String fillRight(String str, int length, char filler)
str - the stringlength - the desired lengthfiller - the filler characterpublic static String firstBefore(String str, char delimiter)
str - the stringdelimiter - the delimiterpublic static String lastAfter(String str, char delimiter)
str - the stringdelimiter - the delimiterpublic static String firstLine(String str)
str - the multiline stringpublic static String toHexString(byte[] binaryData)
binaryData - the data, may be nullpublic static byte[] toBytes(char c)
c - the characterpublic static byte[] toBytes(char[] chars)
chars - the character arraypublic static String hash(String algorithm, char[] salt, char[] input)
toBytes(char[])
before applying the hash.algorithm - the hashing algorithm (MD5, SHA-1, SHA-256 are supported by all java runtimes, at least)salt - the "salt", null if plain hashinput - is the input array of charspublic static String objectToLoggableString(Object object)
object - the objectpublic static int countOccurrences(String str, char c)
str - the string to testc - the character to check forpublic static String objectArrayToString(Object[] objArray, String separator)
objArray - the array of objectsseparator - the string between two objectspublic static char[] toCharArray(String str)
str - the stringpublic static void fill(char[] buf,
char val)
buf - the char buffer, may be nullval - the value to fillArrays.fill(char[], char)public static void blank(char[] buf)
buf - the char buff, may be nullfill(char[], char)public static String toHTML(String text)
text - the input stringpublic static String removeTrailingText(String str, String trail)
str - the original stringtrail - the trailing string to remove, if foundpublic static String toAsciiLetterOrDigit(String str)
unDiacrit(java.lang.String, boolean).
The resulting string will not contain more than one underscore in a row.
Nice to create filenames.
str - the stringTentackle - distributed, domain- and model-driven