Class StringUtil

java.lang.Object
com.sk89q.util.StringUtil

public final class StringUtil extends Object
String utilities.
  • Method Details

    • trimLength

      public static String trimLength(String str, int len)
      Trim a string if it is longer than a certain length.
      Parameters:
      str - the string
      len - the length to trim to
      Returns:
      a new string
    • joinString

      public static String joinString(String[] str, String delimiter, int initialIndex)
      Join an array of strings into a string.
      Parameters:
      str - the string array
      delimiter - the delimiter
      initialIndex - the initial index to start form
      Returns:
      a new string
    • joinQuotedString

      public static String joinQuotedString(String[] str, String delimiter, int initialIndex, String quote)
      Join an array of strings into a string.
      Parameters:
      str - the string array
      delimiter - the delimiter
      initialIndex - the initial index to start form
      quote - the character to put around each entry
      Returns:
      a new string
    • joinString

      public static String joinString(String[] str, String delimiter)
      Join an array of strings into a string.
      Parameters:
      str - the string array
      delimiter - the delimiter
      Returns:
      a new string
    • joinString

      public static String joinString(Object[] str, String delimiter, int initialIndex)
      Join an array of strings into a string.
      Parameters:
      str - an array of objects
      delimiter - the delimiter
      initialIndex - the initial index to start form
      Returns:
      a new string
    • joinString

      public static String joinString(int[] str, String delimiter, int initialIndex)
      Join an array of strings into a string.
      Parameters:
      str - a list of integers
      delimiter - the delimiter
      initialIndex - the initial index to start form
      Returns:
      a new string
    • joinString

      public static String joinString(Collection<?> str, String delimiter, int initialIndex)
      Join an list of strings into a string.
      Parameters:
      str - a list of strings
      delimiter - the delimiter
      initialIndex - the initial index to start form
      Returns:
      a new string
    • getLevenshteinDistance

      public static int getLevenshteinDistance(String s, String t)

      Find the Levenshtein distance between two Strings.

      This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).

      The previous implementation of the Levenshtein distance algorithm was from http://www.merriampark.com/ld.htm

      Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError which can occur when my Java implementation is used with very large strings.
      This implementation of the Levenshtein distance algorithm is from http://www.merriampark.com/ldjava.htm

       StringUtil.getLevenshteinDistance(null, *)             = IllegalArgumentException
       StringUtil.getLevenshteinDistance(*, null)             = IllegalArgumentException
       StringUtil.getLevenshteinDistance("","")               = 0
       StringUtil.getLevenshteinDistance("","a")              = 1
       StringUtil.getLevenshteinDistance("aaapppp", "")       = 7
       StringUtil.getLevenshteinDistance("frog", "fog")       = 1
       StringUtil.getLevenshteinDistance("fly", "ant")        = 3
       StringUtil.getLevenshteinDistance("elephant", "hippo") = 7
       StringUtil.getLevenshteinDistance("hippo", "elephant") = 7
       StringUtil.getLevenshteinDistance("hippo", "zzzzzzzz") = 8
       StringUtil.getLevenshteinDistance("hello", "hallo")    = 1
       
      Parameters:
      s - the first String, must not be null
      t - the second String, must not be null
      Returns:
      result distance
      Throws:
      IllegalArgumentException - if either String input null
    • lookup

      public static <T extends Enum<?>> T lookup(Map<String,T> lookup, String name, boolean fuzzy)
    • parseListInQuotes

      public static List<String> parseListInQuotes(String[] input, char delimiter, char quoteOpen, char quoteClose)
    • parseListInQuotes

      public static List<String> parseListInQuotes(String[] input, char delimiter, char quoteOpen, char quoteClose, boolean appendLeftover)
    • split

      public static List<String> split(String input, char delimiter, char open, char close)
      Splits a string respecting enclosing quotes.
      Parameters:
      input - the input to split.
      delimiter - the delimiter to split on.
      open - the opening quote character.
      close - the closing quote character.
      Returns:
      a list of split strings.