Package eu.bandm.tools.util.java
Class Strings
java.lang.Object
eu.bandm.tools.util.java.Strings
Utility class for string manipulation.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic String
capitalize
(String s) Turns the very first character to upper case.static <T> String
collectionToText
(Collection<T> elements, Function<T, String> fun, String separatorNotLast, String separatorLast, String separatorOnly) Convert a collection into a human-readable string representation by applying the given function to all elements and merging the results.static String
Compose two Strings with"."
as the standard separator.static int
editDistance
(String s1, String s2) Computes the Levenshtein distance of two strings.static String
Delivers easily readable String representation.static <T> String
iterableToString
(@Opt String openDelim, Iterable<T> elements, String separator, @Opt String closeDelim) Convert an Iterable into a string representation by applyingObject.toString()
to all elements and merging the results.static <T> String
iterableToString
(@Opt String openDelim, Iterable<T> elements, Function<T, String> fun, String separator, @Opt String closeDelim) Convert an Iterable into a string representation by applying the given function to all elements and merging the results.static String
Replace linefeed according to different encodings, plus maximal preceding whitespace, by one standard Unix encoding.static String
Deliver the prefix of the given string of the given max length.static String
Deliver the prefix of the given string of the given max length.static String
removeQuotes
(String in) Remove the first and the last character in a string.static String
Replace linefeed according to different encodings, plus maximal preceding whitespace, by one single space character.static String[]
splitByFirst
(String text) Takes the very first character as delimiter for splitting the rest of the string.static String
Turns the very first character to lower case.
-
Field Details
-
linefeedPattern
Regular expression which matches different kinds of line feeds and preceding whitespace. The linefeeds encodings are"\n"
,"\r"
, and\r\n
. The replacement is"\n"
. -
STANDARD_SUFFIX
Will be appended to a truncated string.- See Also:
-
-
Method Details
-
removeQuotes
Remove the first and the last character in a string. (Assuming that these are open and close quotations marks.)- Parameters:
in
- the String from which to remove the quote characters.- Returns:
- the String without the quote characters.
-
normalizeLinefeed
Replace linefeed according to different encodings, plus maximal preceding whitespace, by one standard Unix encoding. The replaced encodings are"\n"
,"\r"
, and\r\n
. The replacement is"\n"
. The preceding whitespace is a maximal sequence of blanks, tabs, and formfeeds.(Please note that there are other linefeed encodings, not covered by this method and seldomly used, like IBM mainframe 0x85.)
- Parameters:
s
- the String in which to replace linefeeds and preceding whitespace.- Returns:
- the String with normalized linefeeds and preceding whitespace.
-
replaceLinefeedByBlank
Replace linefeed according to different encodings, plus maximal preceding whitespace, by one single space character. The replaced encodings are"\n"
,"\r"
, and\r\n
. The replacement is" "
. The preceding whitespace is a maximal sequnce of blanks, tabs, and formfeeds.(Please note that there are other linefeed encodings, not covered by this method and seldomly used, like IBM mainframe 0x85.)
- Parameters:
s
- the String in which to replace linefeeds and preceding whitespace.- Returns:
- the String with normalized linefeeds and preceding whitespace.
-
prefix
Deliver the prefix of the given string of the given max length. Append the suffix if the input must be truncated. In this case the result (including the suffix) will have the given length.- Parameters:
s
- the String to truncate.len
- the maximal length of the result.suffix
- the String to append to a truncated input.- Returns:
- the decorated prefix of the give string.
- Throws:
IllegalArgumentException
- if len < 0 or suffix longer than len.
-
prefix
Deliver the prefix of the given string of the given max length. Append theSTANDARD_SUFFIX
if the input has been truncated.- Parameters:
s
- the String to truncate.len
- the maximal length of the result.- Returns:
- the decorated prefix of the give string.
- Throws:
IllegalArgumentException
- if len < 0 or suffix longer than len.- See Also:
-
hash32
Delivers easily readable String representation. The hashcode of the argument is printed based with 32 digits, i.e.'0'
to'k'
.- Parameters:
o
- the object to print.- Returns:
- the String representation.
-
compose
Compose two Strings with"."
as the standard separator.- Parameters:
prefix
- the first string.suffix
- the second string.- Returns:
- the concatenation or one of the Strings unaltered.
- See Also:
-
uncapitalize
Turns the very first character to lower case. For this,Locale.ROOT
is used. All other characters are unaltered.- Parameters:
s
- the string to uncapitalize.- Returns:
- the uncapitalized string.
-
capitalize
Turns the very first character to upper case. For this,Locale.ROOT
is used. All other characters are unaltered.- Parameters:
s
- the string to capitalize.- Returns:
- the capitalized string.
-
splitByFirst
Takes the very first character as delimiter for splitting the rest of the string. The last character is ignored if it is the delimiter, so no empty sub-string is recognized at the end of the list. Is somehow equivalent to the xslt codesplitbyfirst
indoctypes/d2d_gp/libbasic.xslt
.- Parameters:
text
- the string to split.- Returns:
- the splitted string.
-
iterableToString
public static <T> String iterableToString(@Opt @Opt String openDelim, Iterable<T> elements, String separator, @Opt @Opt String closeDelim) Convert an Iterable into a string representation by applyingObject.toString()
to all elements and merging the results.- Type Parameters:
T
- the type of the items to print.- Parameters:
openDelim
- prefix of the result, if not null.elements
- the String representation of which shall be concatenated.separator
- is inserted between two elements.closeDelim
- suffix of the result, if not null.- Returns:
- the composed string.
- See Also:
-
iterableToString
public static <T> String iterableToString(@Opt @Opt String openDelim, Iterable<T> elements, Function<T, String> fun, String separator, @Opt @Opt String closeDelim) Convert an Iterable into a string representation by applying the given function to all elements and merging the results.- Type Parameters:
T
- the type of the items to print.- Parameters:
openDelim
- prefix of the result, if not null.elements
- the String representation of which shall be concatenated.fun
- delivers the String representation of the elements.separator
- is inserted between two elements.closeDelim
- suffix of the result, if not null.- Returns:
- the composed string.
-
collectionToText
public static <T> String collectionToText(Collection<T> elements, Function<T, String> fun, String separatorNotLast, String separatorLast, String separatorOnly) Convert a collection into a human-readable string representation by applying the given function to all elements and merging the results. Is required for printed English language, for results like »a, b, and c« and »a and b«.- Type Parameters:
T
- the type of the elements.- Parameters:
elements
- to display.fun
- to apply.separatorNotLast
- separator between multiple elements.separatorLast
- separator between the last two elements.separatorOnly
- separator between only two elements.- Returns:
- a human-readable string representation of the collection.
-
editDistance
Computes the Levenshtein distance of two strings.- Parameters:
s1
- one strings2
- another string- Returns:
- the Levenshtein (edit) distance of the two strings
- See Also:
-