Class StringUtil

java.lang.Object
net.okapia.osid.torrefacto.util.StringUtil

public class StringUtil extends Object
Some string utilities.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new StringUtil.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    abbreviate(String str, int maxWidth)
    Abbreviates a string using ellipses.
    static String
    abbreviate(String str, int offset, int maxWidth)
    Abbreviates a string using ellipses, working from the given left edge offset so that a window further into the string can be shown.
    static String
    abbreviate(String str, String abbrevMarker, int maxWidth)
    Abbreviates a string using the given replacement marker.
    static String
    abbreviate(String str, String abbrevMarker, int offset, int maxWidth)
    Abbreviates a string using the given replacement marker, working from the given left edge offset.
    static String
    abbreviate(DisplayText text, int maxWidth)
    Abbreviates the text of a display text using ellipses.
    static String
    abbreviate(DisplayText text, int offset, int maxWidth)
    Abbreviates the text of a display text using ellipses, working from the given left edge offset.
    static String
    abbreviate(DisplayText text, String abbrevMarker, int maxWidth)
    Abbreviates the text of a display text using the given replacement marker.
    static String
    abbreviate(DisplayText text, String abbrevMarker, int offset, int maxWidth)
    Abbreviates the text of a display text using the given replacement marker, working from the given left edge offset.
    static String
    abbreviateMiddle(String str, String middle, int length)
    Abbreviates a string to the given length using a replacement marker in the middle, keeping the first and last characters.
    static String
    abbreviateMiddle(DisplayText text, String middle, int length)
    Abbreviates the text of a display text to the given length using a replacement marker in the middle.
    static String
    Capitalizes a string.
    static String
    Capitalizes the text of a display text.
    static String
    center(String str, int size)
    Centers a string in a larger string of the given size, padding with spaces.
    static String
    center(String str, int size, char padChar)
    Centers a string in a larger string of the given size, padding with the given character.
    static String
    center(String str, int size, String padStr)
    Centers a string in a larger string of the given size, padding with the given string.
    static String
    center(DisplayText text, int size)
    Centers the text of a display text in a larger string of the given size, padding with spaces.
    static String
    center(DisplayText text, int size, char padChar)
    Centers the text of a display text in a larger string of the given size, padding with the given character.
    static String
    center(DisplayText text, int size, String padStr)
    Centers the text of a display text in a larger string of the given size, padding with the given string.
    static String
    Gets a lower case article for the given word.
    static String
    Gets a lower case article for the text of a display text.
    static String
    Gets an upper case article for the given word.
    static String
    Gets an upper case article for the text of a display text.
    static boolean
    Tests if a string is null or empty ("").
    static boolean
    Tests if the text of a display text is null or empty ("").
    static boolean
    Tests if a string contains only Unicode digits.
    static boolean
    Tests if the text of a display text contains only Unicode digits.
    static boolean
    isVowel(char c)
    Tests if a letter is a vowel.
    static String
    ordinalize(long number)
    Turns a non-negative number into an ordinal string used to denote the position in an ordered sequence, such as 1st, 2nd, 3rd, 4th.
    static String
    UnCapitalizes a String.
    static String
    Uncapitalizes the text of a display text.

    Methods inherited from class Object

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

    • StringUtil

      public StringUtil()
      Constructs a new StringUtil.
  • Method Details

    • getUpperArticle

      public static String getUpperArticle(DisplayText text)
      Gets an upper case article for the text of a display text.
      Parameters:
      text - a display text
      Returns:
      the article
      Throws:
      NullArgumentException - text is null
    • getUpperArticle

      public static String getUpperArticle(String s)
      Gets an upper case article for the given word.
      Parameters:
      s - a work or phrase
      Returns:
      the article
      Throws:
      NullArgumentException - s is null
    • getLowerArticle

      public static String getLowerArticle(DisplayText text)
      Gets a lower case article for the text of a display text.
      Parameters:
      text - a display text
      Returns:
      the article
      Throws:
      NullArgumentException - text is null
    • getLowerArticle

      public static String getLowerArticle(String s)
      Gets a lower case article for the given word.
      Parameters:
      s - a work or phrase
      Returns:
      the article
      Throws:
      NullArgumentException - s is null
    • capitalize

      public static String capitalize(DisplayText text)
      Capitalizes the text of a display text.
      Parameters:
      text - a display text
      Returns:
      the capitalized string
      Throws:
      NullArgumentException - text is null
    • capitalize

      public static String capitalize(String str)
      Capitalizes a string.
      Parameters:
      str - a string
      Returns:
      the capitalized string
      Throws:
      NullArgumentException - str is null
    • uncapitalize

      public static String uncapitalize(DisplayText text)
      Uncapitalizes the text of a display text.
      Parameters:
      text - a display text
      Returns:
      the uncapitalized string
      Throws:
      NullArgumentException - text is null
    • uncapitalize

      public static String uncapitalize(String str)
      UnCapitalizes a String.
      Parameters:
      str - a string
      Returns:
      the uncapitalized string
      Throws:
      NullArgumentException - str is null
    • ordinalize

      public static String ordinalize(long number)
      Turns a non-negative number into an ordinal string used to denote the position in an ordered sequence, such as 1st, 2nd, 3rd, 4th.
      Parameters:
      number - the non-negative number
      Returns:
      the string with the number and ordinal suffix
      Throws:
      InvalidArgumentException - number is negative
    • isVowel

      public static boolean isVowel(char c)
      Tests if a letter is a vowel.
      Parameters:
      c - a character
      Returns:
      true if c is a vowelm false otherwise
    • abbreviate

      public static String abbreviate(DisplayText text, int maxWidth)
      Abbreviates the text of a display text using ellipses.
      Parameters:
      text - a display text
      maxWidth - the maximum length of the result, at least 4
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - text is null
    • abbreviate

      public static String abbreviate(String str, int maxWidth)
      Abbreviates a string using ellipses. The result is no longer than maxWidth. For example:
      abbreviate("abcdefg", 6) = "abc..."
      abbreviate("abcdefg", 7) = "abcdefg"
      abbreviate("abcdefg", 4) = "a..."
      
      Parameters:
      str - the string to abbreviate
      maxWidth - the maximum length of the result, at least 4
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - str is null
    • abbreviate

      public static String abbreviate(DisplayText text, int offset, int maxWidth)
      Abbreviates the text of a display text using ellipses, working from the given left edge offset.
      Parameters:
      text - a display text
      offset - the left edge of the source string
      maxWidth - the maximum length of the result, at least 4
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - text is null
    • abbreviate

      public static String abbreviate(String str, int offset, int maxWidth)
      Abbreviates a string using ellipses, working from the given left edge offset so that a window further into the string can be shown. For example:
      abbreviate("abcdefghijklmno", 5, 10) = "...fghi..."
      
      Parameters:
      str - the string to abbreviate
      offset - the left edge of the source string
      maxWidth - the maximum length of the result, at least 4
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - str is null
    • abbreviate

      public static String abbreviate(DisplayText text, String abbrevMarker, int maxWidth)
      Abbreviates the text of a display text using the given replacement marker.
      Parameters:
      text - a display text
      abbrevMarker - the replacement marker, may be null
      maxWidth - the maximum length of the result
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - text is null
    • abbreviate

      public static String abbreviate(String str, String abbrevMarker, int maxWidth)
      Abbreviates a string using the given replacement marker. The result is no longer than maxWidth. For example:
      abbreviate("abcdefg", ".", 5) = "abcd."
      
      Parameters:
      str - the string to abbreviate
      abbrevMarker - the replacement marker, may be null
      maxWidth - the maximum length of the result
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - str is null
    • abbreviate

      public static String abbreviate(DisplayText text, String abbrevMarker, int offset, int maxWidth)
      Abbreviates the text of a display text using the given replacement marker, working from the given left edge offset.
      Parameters:
      text - a display text
      abbrevMarker - the replacement marker, may be null
      offset - the left edge of the source string
      maxWidth - the maximum length of the result
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - text is null
    • abbreviate

      public static String abbreviate(String str, String abbrevMarker, int offset, int maxWidth)
      Abbreviates a string using the given replacement marker, working from the given left edge offset. A null string returns null and an empty marker with a positive width simply truncates. For example:
      abbreviate("abcdefghijklmno", "...", 5, 10) = "...fghi..."
      abbreviate("abcdefghijklmno", "...", 8, 10) = "...ijklmno"
      
      Parameters:
      str - the string to abbreviate
      abbrevMarker - the replacement marker, may be null
      offset - the left edge of the source string
      maxWidth - the maximum length of the result
      Returns:
      the abbreviated string
      Throws:
      InvalidArgumentException - maxWidth is too small
      NullArgumentException - str is null
    • abbreviateMiddle

      public static String abbreviateMiddle(DisplayText text, String middle, int length)
      Abbreviates the text of a display text to the given length using a replacement marker in the middle.
      Parameters:
      text - a display text
      middle - the replacement marker, may be null
      length - the target length
      Returns:
      the abbreviated string, or the original if it does not fit
      Throws:
      NullArgumentException - text is null
    • abbreviateMiddle

      public static String abbreviateMiddle(String str, String middle, int length)
      Abbreviates a string to the given length using a replacement marker in the middle, keeping the first and last characters. The string is returned unchanged if it is shorter than the length or the length cannot accommodate the marker. For example:
      abbreviateMiddle("abcdef", ".", 4) = "ab.f"
      
      Parameters:
      str - the string to abbreviate
      middle - the replacement marker, may be null
      length - the target length
      Returns:
      the abbreviated string, or the original if it does not fit
      Throws:
      NullArgumentException - str is null
    • center

      public static String center(DisplayText text, int size)
      Centers the text of a display text in a larger string of the given size, padding with spaces.
      Parameters:
      text - a display text
      size - the size of the result
      Returns:
      the centered string
      Throws:
      NullArgumentException - text is null
    • center

      public static String center(String str, int size)
      Centers a string in a larger string of the given size, padding with spaces. The original string is returned when the size is smaller. For example:
      center("abc", 7) = "  abc  "
      center("ab", 4)  = " ab "
      
      Parameters:
      str - the string to center
      size - the size of the result
      Returns:
      the centered string
      Throws:
      NullArgumentException - str is null
    • center

      public static String center(DisplayText text, int size, char padChar)
      Centers the text of a display text in a larger string of the given size, padding with the given character.
      Parameters:
      text - a display text
      size - the size of the result
      padChar - the padding character
      Returns:
      the centered string
      Throws:
      NullArgumentException - text is null
    • center

      public static String center(String str, int size, char padChar)
      Centers a string in a larger string of the given size, padding with the given character.
      Parameters:
      str - the string to center
      size - the size of the result
      padChar - the padding character
      Returns:
      the centered string
      Throws:
      NullArgumentException - str is null
    • center

      public static String center(DisplayText text, int size, String padStr)
      Centers the text of a display text in a larger string of the given size, padding with the given string.
      Parameters:
      text - a display text
      size - the size of the result
      padStr - the padding string, a space if empty
      Returns:
      the centered string
      Throws:
      NullArgumentException - text is null
    • center

      public static String center(String str, int size, String padStr)
      Centers a string in a larger string of the given size, padding with the given string. An empty pad string defaults to a space. For example:
      center("abc", 7, "*") = "**abc**"
      
      Parameters:
      str - the string to center
      size - the size of the result
      padStr - the padding string, a space if empty
      Returns:
      the centered string
      Throws:
      NullArgumentException - str is null
    • isNumeric

      public static boolean isNumeric(DisplayText text)
      Tests if the text of a display text contains only Unicode digits.
      Parameters:
      text - a display text
      Returns:
      true if the text is non-empty and contains only digits, false otherwise
      Throws:
      NullArgumentException - text is null
    • isNumeric

      public static boolean isNumeric(String s)
      Tests if a string contains only Unicode digits. A null or empty string returns false; a sign or decimal point is not a digit. For example:
      isNumeric("123")  = true
      isNumeric("12.3") = false
      isNumeric("-123") = false
      
      Parameters:
      s - a string
      Returns:
      true if s is non-empty and contains only digits, false otherwise
    • isEmpty

      public static boolean isEmpty(DisplayText text)
      Tests if the text of a display text is null or empty ("").
      Parameters:
      text - a display text
      Returns:
      true if the text is null or empty, false otherwise
      Throws:
      NullArgumentException - text is null
    • isEmpty

      public static boolean isEmpty(String s)
      Tests if a string is null or empty ("").
      Parameters:
      s - a string
      Returns:
      true if s is null or empty, false otherwise