Class RhinoStr

java.lang.Object
org.apache.commons.lang3.StringUtils
io.higson.runtime.rhino.RhinoStr

public class RhinoStr extends org.apache.commons.lang3.StringUtils
Author:
przemek hertel
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final RhinoStr
     

    Fields inherited from class org.apache.commons.lang3.StringUtils

    CR, EMPTY, INDEX_NOT_FOUND, LF, SPACE
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Capitalizes first letter of all words in the provided String.
    static String
    Capitalizes the first letter of the provided argument.
    static String
    Changes all occurrences of adjacent spaces into one space.
    static String
    concat(Object[] arr)
    Returns comma-separated string representations of array elements.
    static String
    concat(Object[] arr, String delim)
    Returns the string representations of array elements, concatenated into one String, separated by provided delimiter.
    static String
    concat(Object[] arr, String delim, String nullReplacement)
    Returns the string representations of array elements, concatenated into one String, separated by provided delimiter.
    static String
    concat(List<?> list)
    Returns the string representations of elements of the List, concatenated into one comma-separated String.
    static String
    concat(List<?> list, String delim)
    Returns the string representations of elements of the List, concatenated into one String, separated by provided delimiter.
    static boolean
    contains(String[] arr, String element)
    Checks if an array of Strings contains the specified element.
    static boolean
    contains(Collection<?> coll, String fragment)
    Checks if the collection contains an object, whose String representation contains the specified fragment, ignoring letter case.
    static int
    count(String s, char c)
    Counts the occurrences of character in String
    static String
    filterChars(String s, char lower, char upper)
    Quick method removing from the given String all characters not between lower (inclusive) and upper (inclusive).
    static String
    filterChars(String s, String allowed)
    A quick method removing from the given String all characters not appearing in the second String argument.
    static String
    Quick method removing from the given String all non-digits.
    static String
    format(String pattern, Object... args)
    Performs argument substitution for the pattern passed as parameter.
    static String
    Returns the longest common prefix of two Strings
    static int
    len(String str)
    Returns the length of the String or 0 if the string is null.
    static String
    Converts null Strings to empty strings.
    static String
    nvl(String val)
    Returns the argument if it is not null.
    static String
    padLeft(String s, char c, int len)
    Returns the String argument 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 String
    padLeft(String s, int len)
    Returns the String argument 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 String
    padRight(String s, char c, int len)
    Returns the String argument 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 String
    padRight(String s, int len)
    Returns the String argument 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 String
    print(Object list)
    Creates a String representing the content of Collection, array or Iterable in a clear form of a list of items, with each item in separate line.
    static String
    print(Object list, String title)
    Creates a String representing the content of Collection, array or Iterable in a clear form of a list of items, with each item in separate line.
    static String
    print(Object list, String title, int maxLines)
    Creates a String representing the content of Collection, array or Iterable in a clear form of a list of items, with each item in separate line.
    static String
    rejectChars(String s, String rejected)
    A quick method removing from the given String all characters appearing in the second String argument.
    static String
    repeat(char c, int count)
    Returns the String consisting of the given character repeated specified number of times.
    static String[]
    split(String str, char delim)
    Splits the given string into substrings delimited by specified single character and returns an array of substrings.
    static String[]
    split(String str, char delim, int max)
    Splits the given string into substrings delimited by specified single character.
    static String
    Trims the argument from leading and trailing whitespaces if it's not null.
    static String
    trunc(String s, int maxlen)
    If necessary, shortens the given String to 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • INSTANCE

      public static final RhinoStr INSTANCE
    • CONCAT_INITIAL_CAPACITY

      public static final int CONCAT_INITIAL_CAPACITY
      See Also:
  • Method Details

    • compactSpaces

      public static String compactSpaces(String s)
      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 null if the argument is null.
    • filterDigits

      public static String filterDigits(String s)
      Quick method removing from the given String all non-digits. An alternative to slow method s.replaceAll(s1,s2) (7-8 times quicker)
      Parameters:
      s - the String to clear of all non-digit characters.
      Returns:
      s with removed all characters not between '0' and '9'. If s is null, the method returns null.
    • filterChars

      public static String filterChars(String s, char lower, char upper)
      Quick method removing from the given String all characters not between lower (inclusive) and upper (inclusive).
      Parameters:
      s - the String to clear of all characters not within provided bounds.
      lower - lower bound for retained characters.
      upper - upper bound for retained characters.
      Returns:
      s with removed all characters not between lower and upper. If s is null, the method returns null.
    • filterChars

      public static String filterChars(String s, String allowed)
      A quick method removing from the given String all characters not appearing in the second String argument.
      Parameters:
      s - the String to be cleared of all characters not appearing in the second argument.
      allowed - String containing characters to retain in the first argument.
      Returns:
      s with removed all characters not appearing in second argument. Returns null if either of the arguments is null.
      See Also:
    • rejectChars

      public static String rejectChars(String s, String rejected)
      A quick method removing from the given String all characters appearing in the second String argument.
      Parameters:
      s - the String to be cleared of all characters appearing in the second argument.
      rejected - String containing characters to remove from the first argument.
      Returns:
      s with removed all characters from the second argument. Returns null if either of the arguments is null.
    • capitalizeFirst

      public static String capitalizeFirst(String text)
      Capitalizes the first letter of the provided argument.
      Parameters:
      text - The text to capitalize the first letter.
      Returns:
      text with the first letter changed to upper case. Returns unchanged argument if it is null or empty.
    • capitalizeAll

      public static String capitalizeAll(String str)
      Capitalizes first letter of all words in the provided String. Changes all occurences of adjacent whitespaces to single spaces. Trims the result.
      Parameters:
      str - the String to 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 returns null.

    • concat

      public static String concat(Object[] arr)
      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 String with list of string representations of elements from arr, in the order they appear in the array. The list is separated with comma (','). String representations of objects are obtained by calling their toString method. If an element of the array is null, its string representation is "null".

      If the provided array is null, the result is an empty String.

      See Also:
    • concat

      public static String concat(Object[] arr, String delim)
      Returns the string representations of array elements, concatenated into one String, 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 String with list of string representations of elements from arr, in the order they appear in the array. The list is separated with delim. String representations of objects are obtained by calling their toString method. If an element of the array is null, its string representation is "null".

      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".

      See Also:
    • concat

      public static String concat(Object[] arr, String delim, String nullReplacement)
      Returns the string representations of array elements, concatenated into one String, separated by provided delimiter. null elements 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 list
      nullReplacement - text representation of null elements of the array.
      Returns:
      a String with list of string representations of elements from arr, in the order they appear in the array. The list is separated with delim. String representations of objects are obtained by calling their toString method. If an element of the array is null, its string representation is provided in the nullReplacement argument.

      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 nullReplacement argument is null, the null elements of the array will be represented by "null" string.

      See Also:
    • concat

      public static String concat(List<?> list, String delim)
      Returns the string representations of elements of the List, concatenated into one String, separated by provided delimiter.
      Parameters:
      list - List of elements whose string representations will be included in the result.
      delim - separator of elements.
      Returns:
      a String with text representations of elements from the list, in the order they appear on the list. The text representations of elements will be separated by delim. Text representations of objects are obtained by calling their toString method. If an element of the list is null, 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

      public static String concat(List<?> list)
      Returns the string representations of elements of the List, concatenated into one comma-separated String.
      Parameters:
      list - List of elements whose string representations will be included in the result.
      Returns:
      a String with 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 their toString method. If an element of the list is null, its string representation is "null".

      If the provided list is null, the result is an empty String.

      See Also:
    • getCommonStart

      public static String getCommonStart(String s1, String s2)
      Returns the longest common prefix of two Strings

      Example:

      getCommonStart("abcd", "abdd") returns "ab".

      Parameters:
      s1 - the first String
      s2 - the second String
      Returns:
      null if either of the arguments is null. Otherwise returns the longest common prefix of the two strings.
    • padLeft

      public static String padLeft(String s, int len)
      Returns the String argument 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 the String argument 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 - the String argument.
      len - minimum length of the resulting String.
      Returns:
      s if length of s is greater than or equal to len. If length of s is less than len, the result will be s with spaces added at the end, so that the total length of the result will be equal len. If s is null, the result will be null.
    • padLeft

      public static String padLeft(String s, char c, int len)
      Returns the String argument 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 the String argument already has length equal to or greater than the provided minimum length, it will be returned unchanged.
      Parameters:
      s - the String argument.
      c - the character to be added at the end of the String argument to increase its length to. len, if necessary.
      len - minimum length of the resulting String.
      Returns:
      s if length of s is greater than or equal to len. If length of s is less than len, the result will be s with c added at the end, so that the total length of the result will be equal len. If s is null, the result will be null.
    • padRight

      public static String padRight(String s, int len)
      Returns the String argument 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 the String argument 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 - the String argument.
      len - minimum length of the resulting String.
      Returns:
      s if length of s is greater than or equal to len. If length of s is less than len, the result will be s with spaces added at the beginning required number of times, so that the total length of the result will be equal len. If s is null, the result will be null.
    • padRight

      public static String padRight(String s, char c, int len)
      Returns the String argument 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 the String argument already has length equal to or greater than the provided minimum length, it will be returned unchanged.
      Parameters:
      s - the String argument.
      c - the character to be added at the beginning of the String argument to increase its length to len, if necessary.
      len - minimum length of the resulting String.
      Returns:
      s if length of s is greater than or equal to len. If length of s is less than len, the result will be s with c added at the beginning required number of times, so that the total length of the result will be equal len. If s is null, the result will be null.
    • notNull

      public static String notNull(String str)
      Converts null Strings to empty strings. Non-null strings are returned unchanged.
      Parameters:
      str - the argument.
      Returns:
      empty string ("") if str is not null. Returns empty string if s is null.
    • trim

      public static String trim(String s)
      Trims the argument from leading and trailing whitespaces if it's not null.
      Parameters:
      s - the String to trim.
      Returns:
      null if s is null. If s is not null, returns s trimmed with String.trim()
    • print

      public static String print(Object list, String title, int maxLines)
      Creates a String representing the content of Collection, array or Iterable in 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 a String. 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:

      The number of lines in the result can be limited by passing the maximum number of lines with data in the last argument, however, the effective limit is always at least 3.

      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 of Objects or Iterable containing 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. If title is null, 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. If maxLines == 0 it is assumed that the number of lines is unlimited. If maxLines < 3, the actual limit on the number of data lines is 3.
      Returns:
      the String representation of the data contained in the first item. If list is not an instance of a Collection, Iterable, array of elements of non-primitive type or list is null, the result will be null.
      Throws:
      NegativeArraySizeException - if maxLines < 0
      See Also:
    • print

      public static String print(Object list, String title)
      Creates a String representing the content of Collection, array or Iterable in 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 a String. 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:

      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 of Objects or Iterable containing 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. If title is null, the line with heading will not be included and the result will contain only data items.
      Returns:
      the String representation of the data contained in the first item. If list is not an instance of a Collection, Iterable, array of elements of non-primitive type or list is null, the result will be null.
      See Also:
    • print

      public static String print(Object list)
      Creates a String representing the content of Collection, array or Iterable in 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 a String. 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:

      Example:

      print(new Integer[]{1,2,3,4}, 4)
      will return
      (4)
         1. 1
         2. 2
         3. 3
         4. 4
      
      Parameters:
      list - Collection, array of Objects or Iterable containing elements to include in the list.
      Returns:
      the String representation of the data contained in the first item. If list is not an instance of a Collection, Iterable, array of elements of non-primitive type or list is null, the result will be null.
      See Also:
    • len

      public static int len(String str)
      Returns the length of the String or 0 if the string is null.
      Parameters:
      str - the argument.
      Returns:
      0 if str is null or length of str if it is not null.
    • count

      public static int count(String s, char c)
      Counts the occurrences of character in String
      Parameters:
      s - the String in which the occurrences of c are counted.
      c - the character whose occurences are counted.
      Returns:
      number of times c occurs in s. If s is null, returns 0.
    • repeat

      public static String repeat(char c, int count)
      Returns the String consisting of the given character repeated specified number of times.
      Parameters:
      c - the character to repeat in the resulting string.
      count - length of the resulting String.
      Returns:
      String consisting of character c repeated count times. If count <= 0, the result is an empty String.
    • contains

      public static boolean contains(Collection<?> coll, String fragment)
      Checks if the collection contains an object, whose String representation contains the specified fragment, ignoring letter case. The representation of an object as a string is obtained by calling the object's StringUtils.toString(byte[], java.lang.String) method. The representation of null is the string "null".
      Parameters:
      coll - collection of objects.
      fragment - the fragment sought in String representations of objects from the collection.
      Returns:
      true if and only if the collection contains an object, whose representation as String contains (ignoring letter case) the fragment. If coll is null, the result is false.
      Throws:
      NullPointerException - if fragment is null.
    • contains

      public static boolean contains(String[] arr, String element)
      Checks if an array of Strings contains the specified element.
      Parameters:
      arr - an array of Strings.
      element - the String sought in the array.
      Returns:
      true if and only if the element is not null, the arr contains the element and does not contain null at position lower than the position of the element. If arr is null, the result is false.
      Throws:
      NullPointerException - if arr contains null and does not contain the element at position prior to that of null.
    • split

      public static String[] split(String str, char delim)
      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 than String.split(String) called with single-character separator.
      Parameters:
      str - the String to split into substrings.
      delim - the character separating the substrings.
      Returns:
      An array with substrings of str delimited by single character delim.
      Throws:
      NullPointerException - if str is null.
      See Also:
    • split

      public static String[] split(String str, char delim, int max)
      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 than String.split(String, int) called with single-character separator.
      Parameters:
      str - the String to split into substrings.
      delim - the character separating the substrings.
      max - if positive, contains the maximum length of the resulting array with substrings. If max == 0, the length of the resulting array is unlimited and all substrings are returned.
      Returns:
      An array with substrings of str delimited by single character delim, with maximum array length specified by the max parameter.
      Throws:
      IllegalArgumentException - if max < 0.
      NullPointerException - if str is null.
      See Also:
    • trunc

      public static String trunc(String s, int maxlen)
      If necessary, shortens the given String to the specified length, by removing appropriate number of characters at the end of the string and adding ellipsis ("...").

      If the String argument 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 necessary
      maxlen - maximum length of the resulting string, including the "..." added at the end if the string is truncated.
      Returns:
      s if the length of s is not greater than maxlen. Otherwise returns the string consisting of the first maxlen - 3 characters from s with "..." added at the end.
      Throws:
      NullPointerException - if s is null.
      StringIndexOutOfBoundsException - if length of s exceeds maxlen and maxlen < 3.
    • nvl

      public static String nvl(String val)
      Returns the argument if it is not null. Returns empty string if the argument is null.
      Parameters:
      val - the argument.
      Returns:
      val if val != null or empty string ("") if val == null.
    • format

      public static String format(String pattern, Object... args)
      Performs argument substitution for the pattern passed 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 with toString called on corresponding arguments. The string representation of a null Object is the string "null". If the pattern is null, the result is also null.

      If there are more formatting anchors in the pattern than Object arguments, the unmatched anchors are left unchanged. If there are fewer formatting anchors, the unmatched Object arguments are ignored.