Class RhinoStr
- Author:
- przemek hertel
-
Field Summary
FieldsFields inherited from class org.apache.commons.lang3.StringUtils
CR, EMPTY, INDEX_NOT_FOUND, LF, SPACE -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcapitalizeAll(String str) Capitalizes first letter of all words in the provided String.static StringcapitalizeFirst(String text) Capitalizes the first letter of the provided argument.static StringChanges all occurrences of adjacent spaces into one space.static StringReturns comma-separated string representations of array elements.static StringReturns the string representations of array elements, concatenated into oneString, separated by provided delimiter.static StringReturns the string representations of array elements, concatenated into oneString, separated by provided delimiter.static StringReturns the string representations of elements of theList, concatenated into one comma-separatedString.static StringReturns the string representations of elements of theList, concatenated into oneString, separated by provided delimiter.static booleanChecks if an array ofStrings contains the specified element.static booleancontains(Collection<?> coll, String fragment) Checks if the collection contains an object, whoseStringrepresentation contains the specified fragment, ignoring letter case.static intCounts the occurrences of character inStringstatic StringfilterChars(String s, char lower, char upper) Quick method removing from the givenStringall characters not betweenlower(inclusive) andupper(inclusive).static StringfilterChars(String s, String allowed) A quick method removing from the givenStringall characters not appearing in the secondStringargument.static StringQuick method removing from the givenStringall non-digits.static StringPerforms argument substitution for thepatternpassed as parameter.static StringgetCommonStart(String s1, String s2) Returns the longest common prefix of twoStringsstatic intReturns the length of theStringor0if the string isnull.static StringConverts nullStrings to empty strings.static StringReturns the argument if it is notnull.static StringReturns theStringargument with provided character appended at the end required number of times, so that the length of the resulting string is at least equal to the last argument.static StringReturns theStringargument with spaces added at the end if necessary, so that the length of the resulting string is at least equal to the second argument.static StringReturns theStringargument with provided character added at the beginning required number of times, so that the length of the resulting string is at least equal to the last argument.static StringReturns theStringargument with spaces added at the beginning if necessary, so that the length of the resulting string is at least equal to the second argument.static StringCreates aStringrepresenting the content ofCollection, array orIterablein a clear form of a list of items, with each item in separate line.static StringCreates aStringrepresenting the content ofCollection, array orIterablein a clear form of a list of items, with each item in separate line.static StringCreates aStringrepresenting the content ofCollection, array orIterablein a clear form of a list of items, with each item in separate line.static StringrejectChars(String s, String rejected) A quick method removing from the givenStringall characters appearing in the secondStringargument.static Stringrepeat(char c, int count) Returns theStringconsisting of the given character repeated specified number of times.static String[]Splits the given string into substrings delimited by specified single character and returns an array of substrings.static String[]Splits the given string into substrings delimited by specified single character.static StringTrims the argument from leading and trailing whitespaces if it's not null.static StringIf necessary, shortens the givenStringto the specified length, by removing appropriate number of characters at the end of the string and adding ellipsis ("...").Methods inherited from class org.apache.commons.lang3.StringUtils
abbreviate, abbreviate, abbreviate, abbreviate, abbreviateMiddle, appendIfMissing, appendIfMissingIgnoreCase, capitalize, center, center, center, chomp, chomp, chop, compare, compare, compareIgnoreCase, compareIgnoreCase, contains, contains, containsAny, containsAny, containsAny, containsAnyIgnoreCase, containsIgnoreCase, containsNone, containsNone, containsOnly, containsOnly, containsWhitespace, countMatches, countMatches, defaultIfBlank, defaultIfEmpty, defaultString, defaultString, deleteWhitespace, difference, endsWith, endsWithAny, endsWithIgnoreCase, equals, equalsAny, equalsAnyIgnoreCase, equalsIgnoreCase, firstNonBlank, firstNonEmpty, getBytes, getBytes, getCommonPrefix, getDigits, getFuzzyDistance, getIfBlank, getIfEmpty, getJaroWinklerDistance, getLevenshteinDistance, getLevenshteinDistance, indexOf, indexOf, indexOf, indexOf, indexOfAny, indexOfAny, indexOfAny, indexOfAnyBut, indexOfAnyBut, indexOfDifference, indexOfDifference, indexOfIgnoreCase, indexOfIgnoreCase, isAllBlank, isAllEmpty, isAllLowerCase, isAllUpperCase, isAlpha, isAlphanumeric, isAlphanumericSpace, isAlphaSpace, isAnyBlank, isAnyEmpty, isAsciiPrintable, isBlank, isEmpty, isMixedCase, isNoneBlank, isNoneEmpty, isNotBlank, isNotEmpty, isNumeric, isNumericSpace, isWhitespace, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, join, joinWith, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOfAny, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, lastOrdinalIndexOf, left, leftPad, leftPad, leftPad, length, lowerCase, lowerCase, mid, normalizeSpace, ordinalIndexOf, overlay, prependIfMissing, prependIfMissingIgnoreCase, remove, remove, removeAll, removeEnd, removeEndIgnoreCase, removeFirst, removeIgnoreCase, removePattern, removeStart, removeStart, removeStartIgnoreCase, repeat, repeat, replace, replace, replaceAll, replaceChars, replaceChars, replaceEach, replaceEachRepeatedly, replaceFirst, replaceIgnoreCase, replaceIgnoreCase, replaceOnce, replaceOnceIgnoreCase, replacePattern, reverse, reverseDelimited, right, rightPad, rightPad, rightPad, rotate, split, split, split, splitByCharacterType, splitByCharacterTypeCamelCase, splitByWholeSeparator, splitByWholeSeparator, splitByWholeSeparatorPreserveAllTokens, splitByWholeSeparatorPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, startsWith, startsWithAny, startsWithIgnoreCase, strip, strip, stripAccents, stripAll, stripAll, stripEnd, stripStart, stripToEmpty, stripToNull, substring, substring, substringAfter, substringAfter, substringAfterLast, substringAfterLast, substringBefore, substringBefore, substringBeforeLast, substringBetween, substringBetween, substringsBetween, swapCase, toCodePoints, toEncodedString, toRootLowerCase, toRootUpperCase, toString, trimToEmpty, trimToNull, truncate, truncate, uncapitalize, unwrap, unwrap, upperCase, upperCase, valueOf, wrap, wrap, wrapIfMissing, wrapIfMissing
-
Field Details
-
INSTANCE
-
CONCAT_INITIAL_CAPACITY
public static final int CONCAT_INITIAL_CAPACITY- See Also:
-
-
Method Details
-
compactSpaces
Changes all occurrences of adjacent spaces into one space. Does not trim the result.- Parameters:
s- the argument.- Returns:
- the argument with all occurrences of adjacent spaces changed to one space.
Returns
nullif the argument isnull.
-
filterDigits
Quick method removing from the givenStringall non-digits. An alternative to slow method s.replaceAll(s1,s2) (7-8 times quicker)- Parameters:
s- theStringto clear of all non-digit characters.- Returns:
swith removed all characters not between'0'and'9'. Ifsisnull, the method returnsnull.
-
filterChars
Quick method removing from the givenStringall characters not betweenlower(inclusive) andupper(inclusive).- Parameters:
s- theStringto clear of all characters not within provided bounds.lower- lower bound for retained characters.upper- upper bound for retained characters.- Returns:
swith removed all characters not betweenlowerandupper. Ifsisnull, the method returnsnull.
-
filterChars
A quick method removing from the givenStringall characters not appearing in the secondStringargument.- Parameters:
s- theStringto be cleared of all characters not appearing in the second argument.allowed-Stringcontaining characters to retain in the first argument.- Returns:
swith removed all characters not appearing in second argument. Returnsnullif either of the arguments isnull.- See Also:
-
rejectChars
A quick method removing from the givenStringall characters appearing in the secondStringargument.- Parameters:
s- theStringto be cleared of all characters appearing in the second argument.rejected-Stringcontaining characters to remove from the first argument.- Returns:
swith removed all characters from the second argument. Returnsnullif either of the arguments isnull.
-
capitalizeFirst
Capitalizes the first letter of the provided argument.- Parameters:
text- The text to capitalize the first letter.- Returns:
textwith the first letter changed to upper case. Returns unchanged argument if it isnullor empty.
-
capitalizeAll
Capitalizes first letter of all words in the provided String. Changes all occurences of adjacent whitespaces to single spaces. Trims the result.- Parameters:
str- theStringto capitalize all words.- Returns:
- the argument with capitalized first letter of all words. A word is defined as any number of adjacent non-whitespace
characters. Words are separated from each other by any number of adjacent whitespace characters.
Each substring separating words is replaced by single space. The result is left- and right-trimmed from whitespaces.
If the argument is
null, the method returnsnull.
-
concat
Returns comma-separated string representations of array elements.Example:
concat(new String[]{"a","b","c"})will return"a,b,c"
- Parameters:
arr- array of arguments. String representations of the elements of this array will be included in the list.- Returns:
- a
Stringwith list of string representations of elements fromarr, in the order they appear in the array. The list is separated with comma (','). String representations of objects are obtained by calling theirtoStringmethod. If an element of the array isnull, its string representation is"null".If the provided array is
null, the result is an emptyString. - See Also:
-
concat
Returns the string representations of array elements, concatenated into oneString, separated by provided delimiter.- Parameters:
arr- array of arguments whose string representations are to be included in the list.delim- separator of the list.- Returns:
- a
Stringwith list of string representations of elements fromarr, in the order they appear in the array. The list is separated withdelim. String representations of objects are obtained by calling theirtoStringmethod. If an element of the array isnull, its string representation is"null".If the provided array is
null, the result is an emptyString.If the provided separator is
null, the list will be separated with string"null". - See Also:
-
concat
Returns the string representations of array elements, concatenated into oneString, separated by provided delimiter.nullelements are represented by provided replacement text.- Parameters:
arr- array of arguments whose string representations are to be included in the list.delim- separator of the listnullReplacement- text representation ofnullelements of the array.- Returns:
- a
Stringwith list of string representations of elements fromarr, in the order they appear in the array. The list is separated withdelim. String representations of objects are obtained by calling theirtoStringmethod. If an element of the array isnull, its string representation is provided in thenullReplacementargument.If the provided array is null, the result is an empty
String.If the provided separator is
null, the list will be separated with string"null".If the
nullReplacementargument isnull, thenullelements of the array will be represented by"null"string. - See Also:
-
concat
Returns the string representations of elements of theList, concatenated into oneString, separated by provided delimiter.- Parameters:
list-Listof elements whose string representations will be included in the result.delim- separator of elements.- Returns:
- a
Stringwith text representations of elements from the list, in the order they appear on the list. The text representations of elements will be separated bydelim. Text representations of objects are obtained by calling theirtoStringmethod. If an element of the list isnull, its string representation is"null".If the provided list is null, the result is an empty
String.If the provided separator is
null, the result will be separated with string"null". - See Also:
-
concat
Returns the string representations of elements of theList, concatenated into one comma-separatedString.- Parameters:
list-Listof elements whose string representations will be included in the result.- Returns:
- a
Stringwith text representations of elements from the list, in the order they appear on the list. The text representations of elements will be separated by comma (','). Text representations of objects are obtained by calling theirtoStringmethod. If an element of the list isnull, its string representation is"null".If the provided list is
null, the result is an emptyString. - See Also:
-
getCommonStart
Returns the longest common prefix of twoStringsExample:
getCommonStart("abcd", "abdd")returns"ab".- Parameters:
s1- the firstStrings2- the secondString- Returns:
nullif either of the arguments isnull. Otherwise returns the longest common prefix of the two strings.
-
padLeft
Returns theStringargument with spaces added at the end if necessary, so that the length of the resulting string is at least equal to the second argument. If theStringargument already has length equal to or greater than the provided minimum length, it will be returned unchanged.This function can be used to align
Strings to the left.- Parameters:
s- theStringargument.len- minimum length of the resultingString.- Returns:
sif length ofsis greater than or equal tolen. If length ofsis less thanlen, the result will beswith spaces added at the end, so that the total length of the result will be equallen. Ifsisnull, the result will benull.
-
padLeft
Returns theStringargument with provided character appended at the end required number of times, so that the length of the resulting string is at least equal to the last argument. If theStringargument already has length equal to or greater than the provided minimum length, it will be returned unchanged.- Parameters:
s- theStringargument.c- the character to be added at the end of theStringargument to increase its length to.len, if necessary.len- minimum length of the resultingString.- Returns:
sif length ofsis greater than or equal tolen. If length ofsis less thanlen, the result will beswithcadded at the end, so that the total length of the result will be equallen. Ifsisnull, the result will benull.
-
padRight
Returns theStringargument with spaces added at the beginning if necessary, so that the length of the resulting string is at least equal to the second argument. If theStringargument already has length equal to or greater than the provided minimum length, it will be returned unchanged.This function can be used to align
Strings to the right.- Parameters:
s- theStringargument.len- minimum length of the resultingString.- Returns:
sif length ofsis greater than or equal tolen. If length ofsis less thanlen, the result will beswith spaces added at the beginning required number of times, so that the total length of the result will be equallen. Ifsisnull, the result will benull.
-
padRight
Returns theStringargument with provided character added at the beginning required number of times, so that the length of the resulting string is at least equal to the last argument. If theStringargument already has length equal to or greater than the provided minimum length, it will be returned unchanged.- Parameters:
s- theStringargument.c- the character to be added at the beginning of theStringargument to increase its length tolen, if necessary.len- minimum length of the resultingString.- Returns:
sif length ofsis greater than or equal tolen. If length ofsis less thanlen, the result will beswithcadded at the beginning required number of times, so that the total length of the result will be equallen. Ifsisnull, the result will benull.
-
notNull
Converts nullStrings to empty strings. Non-nullstrings are returned unchanged.- Parameters:
str- the argument.- Returns:
- empty string (
"") ifstris notnull. Returns empty string ifsisnull.
-
trim
Trims the argument from leading and trailing whitespaces if it's not null.- Parameters:
s- theStringto trim.- Returns:
nullifsisnull. Ifsis not null, returnsstrimmed withString.trim()
-
print
Creates aStringrepresenting the content ofCollection, array orIterablein a clear form of a list of items, with each item in separate line. Lines with data start with the index of the element followed by dot and the representation of the element as aString. Lines are separated by OS-dependent line separator. The first line can contain optional title with the total number of elements in parentheses.The rules of converting the elements to
String:- arrays are converted using
Arrays.toString(Object[])method, - non-array elements are converted by calling their
Object.toString()methods, nullelements are represented by"null"string.
If the number of data elements exceeds the effective limit, the all but last line contains an ellipsis (
"...") and the last line contains the last data element. In this case the total number of data lines (excluding the optional header but including the line with"...") is equal to the limit.Example:
print(new Integer[]{1,2,3,4,5,6}, "title", 4)
will return
title (6) 1. 1 2. 2 ... 6. 6- Parameters:
list-Collection, array ofObjects orIterablecontaining elements to include in the list.title- the title printed in the first line of the result. The provided title is followed by space and total count of elements in parentheses. Iftitleisnull, the line with heading will not be included and the result will contain only data items.maxLines- maximum number of data lines in the result, excluding the optional header line. IfmaxLines == 0it is assumed that the number of lines is unlimited. IfmaxLines < 3, the actual limit on the number of data lines is 3.- Returns:
- the
Stringrepresentation of the data contained in the first item. Iflistis not an instance of aCollection,Iterable, array of elements of non-primitive type orlistisnull, the result will benull. - Throws:
NegativeArraySizeException- ifmaxLines < 0- See Also:
- arrays are converted using
-
print
Creates aStringrepresenting the content ofCollection, array orIterablein a clear form of a list of items, with each item in separate line. Lines with data start with the index of the element followed by dot and the representation of the element as aString. Lines are separated by OS-dependent line separator. The first line can contain optional title with the total number of elements in parentheses.The rules of converting the elements to
String:- arrays are converted using
Arrays.toString(Object[])method, - non-array elements are converted by calling their
Object.toString()methods, nullelements are represented by"null"string.
Example:
print(new Integer[]{1,2,3,4}, "title")
will return
title (4) 1. 1 2. 2 3. 3 4. 4- Parameters:
list-Collection, array ofObjects orIterablecontaining elements to include in the list.title- The title printed in the first line of the result. The provided title is followed by space and total count of elements in parentheses. Iftitleisnull, the line with heading will not be included and the result will contain only data items.- Returns:
- the
Stringrepresentation of the data contained in the first item. Iflistis not an instance of aCollection,Iterable, array of elements of non-primitive type orlistisnull, the result will benull. - See Also:
- arrays are converted using
-
print
Creates aStringrepresenting the content ofCollection, array orIterablein a clear form of a list of items, with each item in separate line. Lines with data start with the index of the element followed by dot and the representation of the element as aString. Lines are separated by OS-dependent line separator. The first line contains the total number of elements in parentheses.The rules of converting the elements to
String:- arrays are converted using
Arrays.toString(Object[])method, - non-array elements are converted by calling their
Object.toString()methods, nullelements are represented by"null"string.
Example:
print(new Integer[]{1,2,3,4}, 4)
will return
(4) 1. 1 2. 2 3. 3 4. 4- Parameters:
list-Collection, array ofObjects orIterablecontaining elements to include in the list.- Returns:
- the
Stringrepresentation of the data contained in the first item. Iflistis not an instance of aCollection,Iterable, array of elements of non-primitive type orlistisnull, the result will benull. - See Also:
- arrays are converted using
-
len
Returns the length of theStringor0if the string isnull.- Parameters:
str- the argument.- Returns:
0ifstrisnullor length ofstrif it is notnull.
-
count
Counts the occurrences of character inString- Parameters:
s- theStringin which the occurrences ofcare counted.c- the character whose occurences are counted.- Returns:
- number of times
coccurs ins. Ifsisnull, returns0.
-
repeat
Returns theStringconsisting of the given character repeated specified number of times.- Parameters:
c- the character to repeat in the resulting string.count- length of the resultingString.- Returns:
Stringconsisting of charactercrepeatedcounttimes. Ifcount <= 0, the result is an emptyString.
-
contains
Checks if the collection contains an object, whoseStringrepresentation contains the specified fragment, ignoring letter case. The representation of an object as a string is obtained by calling the object'sStringUtils.toString(byte[], java.lang.String)method. The representation ofnullis the string"null".- Parameters:
coll- collection of objects.fragment- the fragment sought inStringrepresentations of objects from the collection.- Returns:
- true if and only if the collection contains an object, whose representation as
Stringcontains (ignoring letter case) thefragment. Ifcollisnull, the result isfalse. - Throws:
NullPointerException- iffragmentisnull.
-
contains
Checks if an array ofStrings contains the specified element.- Parameters:
arr- an array ofStrings.element- theStringsought in the array.- Returns:
- true if and only if the
elementis notnull, thearrcontains theelementand does not containnullat position lower than the position of theelement. Ifarris null, the result isfalse. - Throws:
NullPointerException- ifarrcontainsnulland does not contain theelementat position prior to that ofnull.
-
split
Splits the given string into substrings delimited by specified single character and returns an array of substrings. This method is about 4.5 times quicker thanString.split(String)called with single-character separator.- Parameters:
str- theStringto split into substrings.delim- the character separating the substrings.- Returns:
- An array with substrings of
strdelimited by single characterdelim. - Throws:
NullPointerException- ifstrisnull.- See Also:
-
split
Splits the given string into substrings delimited by specified single character. Returns an array of substrings with length limited by provided maximum. This method is about 4.5 times quicker thanString.split(String, int)called with single-character separator.- Parameters:
str- theStringto split into substrings.delim- the character separating the substrings.max- if positive, contains the maximum length of the resulting array with substrings. Ifmax == 0, the length of the resulting array is unlimited and all substrings are returned.- Returns:
- An array with substrings of
strdelimited by single characterdelim, with maximum array length specified by themaxparameter. - Throws:
IllegalArgumentException- ifmax < 0.NullPointerException- ifstrisnull.- See Also:
-
trunc
If necessary, shortens the givenStringto the specified length, by removing appropriate number of characters at the end of the string and adding ellipsis ("...").If the
Stringargument is not longer than the specified maximum length, it is returned unchanged.In any case the length of the resulting string (including the added
"...") is no longer than the provided maximum length.- Parameters:
s- the string to truncate, if necessarymaxlen- maximum length of the resulting string, including the"..."added at the end if the string is truncated.- Returns:
sif the length ofsis not greater thanmaxlen. Otherwise returns the string consisting of the firstmaxlen - 3characters fromswith "..." added at the end.- Throws:
NullPointerException- ifsisnull.StringIndexOutOfBoundsException- if length ofsexceedsmaxlenandmaxlen < 3.
-
nvl
Returns the argument if it is notnull. Returns empty string if the argument isnull.- Parameters:
val- the argument.- Returns:
valifval != nullor empty string ("") ifval == null.
-
format
Performs argument substitution for thepatternpassed as parameter.Example:
format("Hi {}. My name is {}.", "Alice", "Bob")will return
"Hi Alice. My name is Bob."
- Parameters:
pattern- the pattern which will be parsed and formatted.args- the arguments to be substituted in place of the consecutive formatting anchors ("{}").- Returns:
- the formatted pattern with formatting anchors
"{}"substituted withtoStringcalled on corresponding arguments. The string representation of anullObjectis the string"null". If thepatternisnull, the result is alsonull.If there are more formatting anchors in the
patternthanObjectarguments, the unmatched anchors are left unchanged. If there are fewer formatting anchors, the unmatchedObjectarguments are ignored.
-