Class StringUtil


  • public class StringUtil
    extends Object
    A collection of String processing utility methods.
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • rightPad

        public static String rightPad​(String s,
                                      int length)
        Returns a copy of s (right padded) with trailing spaces so that it's length is length. Strings already length characters long or longer are not altered.
        Parameters:
        s - input string to be copied and processed
        length - desired final length of padded string
        Returns:
        the resulting padded string
      • leftPad

        public static String leftPad​(String s,
                                     int length)
        Returns a copy of s (left padded) with leading spaces so that it's length is length. Strings already length characters long or longer are not altered.
        Parameters:
        s - input string to be copied and processed
        length - desired final length of padded string
        Returns:
        the resulting padded string
      • toHexString

        public static String toHexString​(byte[] buf)
        Convenience call for toHexString(byte[], String, int), where sep = null; lineLen = Integer.MAX_VALUE.
        Parameters:
        buf - input data for which to generate a hex string
        Returns:
        the hex string
      • toHexString

        public static String toHexString​(byte[] buf,
                                         String sep,
                                         int lineLen)
        Get a text representation of a byte[] as hexadecimal String, where each pair of hexadecimal digits corresponds to consecutive bytes in the array.
        Parameters:
        buf - input data for which to generate a hex string
        sep - separate every pair of hexadecimal digits with this separator, or null if no separation is needed.
        lineLen - break the output String into lines containing output for lineLen bytes.
        Returns:
        the hex string
      • fromHexString

        public static byte[] fromHexString​(String text)
        Convert a String containing consecutive (no inside whitespace) hexadecimal digits into a corresponding byte array. If the number of digits is not even, a '0' will be appended in the front of the String prior to conversion. Leading and trailing whitespace is ignored.
        Parameters:
        text - input text
        Returns:
        converted byte array, or null if unable to convert
      • isEmpty

        public static boolean isEmpty​(String str)
        Checks if a string is empty (ie is null or empty).
        Parameters:
        str - the String to check for being empty or null
        Returns:
        true if empty or null, false otherwise
      • cleanField

        public static String cleanField​(String value)
        Simple character substitution which cleans/removes all � chars from a given String.
        Parameters:
        value - the String to clean
        Returns:
        substituted cleaned string
      • mask

        public static String mask​(String str)
        Mask sensitive strings - passwords, etc.
        Parameters:
        str - input string
        Returns:
        the masked string, all characters replaced by *
      • mask

        public static String mask​(String str,
                                  char mask)
        Mask sensitive strings - passwords, etc.
        Parameters:
        str - input string
        mask - char used for masking
        Returns:
        the masked string, all characters replaced by the mask character
      • mask

        public static String mask​(String str,
                                  Pattern pattern,
                                  char mask)
        Mask sensitive strings - passwords, etc.
        Parameters:
        str - input string
        pattern - pattern which defines capturing groups to be masked in input
        mask - char used for masking
        Returns:
        the masked string, all characters matched in capturing groups of the pattern replaced by the mask character
      • main

        public static void main​(String[] args)