Package org.kissweb

Class StringUtils

java.lang.Object
org.kissweb.StringUtils

public class StringUtils extends Object
This class contains many methods used to manipulate Strings.
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • substring

      public static String substring(String str, int from, int n)
      This method provides the ability to get a substring of a string by providing a starting location and a length.
      Parameters:
      str - the String to be substring'ed
      from - index of the starting position (starting at 0)
      n - number of characters to take
      Returns:
      the sub-string
    • take

      public static String take(String s, int n)
      An APL-like TAKE function. Assures that the string is n characters long.

      If the string is shorter than n it is padded by spaces.

      If the string is longer than n it is cut short.

      If n is negative, the same thing occurs but it starts at the back of the string. Thus padding or cutting of the string occur at the start of the string.
      Parameters:
      s -
      n -
      Returns:
      See Also:
    • take

      public static char[] take(char[] s, int n)
    • strLength

      public static int strLength(char[] v)
    • movmem

      public static char[] movmem(char[] f, int fi, char[] t, int ti, int n)
      Moves characters from one array to another. The two arrays may be the same array. Correctly handles overlapping regions.
      Parameters:
      f - the from or source array
      fi - the index into f to start at
      t - the to or destination array
      ti - the starting index into t
      n - the number of characters to move
      Returns:
      the destination array
    • drop

      public static String drop(String s, int n)
      An APL-like DROP function. Drops n characters from the string.

      If n is positive, n characters get dropped from the beginning of the string.

      If n is negative, n characters get dropped from the end of the string.

      If n is greater than the length of the string, "" is returned.
      Parameters:
      s -
      n -
      Returns:
      See Also:
    • rightStrip

      public static String rightStrip(String s)
      Strip the spaces of the end of a string.
      Parameters:
      s -
      Returns:
    • leftStrip

      public static String leftStrip(String s)
      Strip the spaces at the beginning of a string.
      Parameters:
      s -
      Returns:
    • centerStrip

      public static String centerStrip(String s)
      Strip the spaces on both ends of a string.
      Parameters:
      s -
      Returns:
    • sprintf

      public static String sprintf(String fmt, Object... arg1)
      C-like sprintf method. Format a string according to the format inserting the arguments as placed in the format string.
      Parameters:
      fmt - format string
      arg1 - consecutive values used by the format string
      Returns:
      See Also:
    • join

      public static String join(String[] strings, String separator)
      Join rows or a String array into a String separated by a delimiter.
      Parameters:
      strings -
      separator -
      Returns:
    • isEmpty

      public static boolean isEmpty(String str)
      String null or empty test. Returns true if string is null or empty.
      Parameters:
      str -
      Returns:
    • stringToCharacter

      public static Character stringToCharacter(String s)
      Convert String to Character correctly handling nulls and zero length strings.
      Parameters:
      s -
      Returns:
    • characterToString

      public static String characterToString(Character c)
      Convert Character to String correctly handling nulls.
      Parameters:
      c -
      Returns:
    • split

      public static String[] split(String s, String r)
      Splits a string into an array of substrings based on a specified delimiter character. Unlike the Java version, this one correctly handles empty strings.
      Parameters:
      s - the string to be split
      r - the delimiter regex
      Returns:
      an array of substrings
    • htmlToText

      public static String htmlToText(String html)
      Converts HTML text to plain text. Also corrects some Unicode characters.
      Parameters:
      html - the HTML text to be converted
      Returns:
      the converted plain text
    • findClosestMatch

      public static String findClosestMatch(Vector<String> strings, String input)
      Finds the string from the given vector of strings that most closely matches the input string.

      This method normalizes the input and the vector strings by removing spaces and converting to lowercase before comparing them using the Jaro-Winkler distance algorithm.
      Parameters:
      strings - a vector of strings to compare against the input string
      input - the input string that may contain typos, case, or space differences
      Returns:
      the string from the vector that most closely matches the input string
    • toAscii

      public static String toAscii(String input)
      Converts a string to ASCII by replacing non-ASCII characters with their ASCII approximations and removing other non-ASCII characters.
      Parameters:
      input - the original string
      Returns:
      the ASCII string