Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    This class contains functions to generate, manipulate or use Strings
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String applySha256​(java.lang.String input)
      Applies the SHA-256 algorithm to the given input.
      static java.lang.String bytesToHex​(byte[] bytes)
      Converts a byte array to hexadecimal represented String
      static byte[] chunkInsert​(java.lang.String data, int n, char insert)
      Inserts a char into a string after every n chars.
      static java.lang.String decodeURL​(java.lang.String encoded)
      Decodes escaped special chars from URLs like '%20' to their UTF-8 counterpart
      static java.lang.String getFileExtension​(java.io.File file)
      Returns the file extension of a file.
      static byte[] hexStringToByteArray​(java.lang.String string)
      Converts a hexadecimal string to byte array
      static java.lang.String hexToString​(java.lang.String hexStr)
      Decodes a hexadecimal string
      static java.lang.String padLeft​(java.lang.String string, int totalLength)
      This class adds spaces to the end of the given string so that the total length of the string is equal to totalLength.
      static java.lang.String padRight​(java.lang.String string, int totalLength)
      This class adds spaces to the begin of the given string so that the total length of the string is equal to totalLength.
      static byte[] readFileData​(java.io.File file, int fileLength)
      Reads file into byte array
      static boolean saveToFile​(java.nio.file.Path file, java.lang.String data)
      Saves a string to a file
      static java.lang.String[] splitStringEvery​(java.lang.String s, int interval)
      splits a given string into parts of maximum length of interval
      static java.util.HashMap<java.lang.String,​java.lang.String> stringToMap​(java.lang.String input)
      Translates a String in the form of key=value&key2=value2... to a HashMap.
      • Methods inherited from class java.lang.Object

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

      • StringUtils

        public StringUtils()
    • Method Detail

      • padLeft

        public static java.lang.String padLeft​(java.lang.String string,
                                               int totalLength)
        This class adds spaces to the end of the given string so that the total length of the string is equal to totalLength. If totalLength is less or equal to the strings length no spaces are added and original input is returned. If input string is null, the result will be aswell.
        Parameters:
        string - to pad
        totalLength - of the result string
        Returns:
        string that has been filled up with spaces to the length of totalLength
      • padRight

        public static java.lang.String padRight​(java.lang.String string,
                                                int totalLength)
        This class adds spaces to the begin of the given string so that the total length of the string is equal to totalLength. If totalLength is less or equal to the strings length no spaces are added and original input is returned. If input string is null, the result will be aswell.
        Parameters:
        string - to pad
        totalLength - of the result string
        Returns:
        string that has been filled up with spaces to the length of totalLength
      • decodeURL

        public static java.lang.String decodeURL​(java.lang.String encoded)
        Decodes escaped special chars from URLs like '%20' to their UTF-8 counterpart
        Parameters:
        encoded - URL
        Returns:
        decoded URL
      • applySha256

        public static java.lang.String applySha256​(java.lang.String input)
        Applies the SHA-256 algorithm to the given input. Input gets converted to UTF-8
        Parameters:
        input - to apply the SHA-256 algorithm to
        Returns:
        hexadecimal representation of SHA-256 applied string
      • readFileData

        public static byte[] readFileData​(java.io.File file,
                                          int fileLength)
                                   throws java.io.IOException
        Reads file into byte array
        Parameters:
        file - to be read
        fileLength - of the file
        Returns:
        byte contents of the file
        Throws:
        java.io.IOException - if something fails
      • saveToFile

        public static boolean saveToFile​(java.nio.file.Path file,
                                         java.lang.String data)
        Saves a string to a file
        Parameters:
        file - Path of the file to save the data in
        data - to save
        Returns:
        true if storing the data was successful, false otherwise
      • stringToMap

        public static java.util.HashMap<java.lang.String,​java.lang.String> stringToMap​(java.lang.String input)
        Translates a String in the form of key=value&key2=value2... to a HashMap.
        Parameters:
        input - string
        Returns:
        HashMap representing the input string
      • chunkInsert

        public static byte[] chunkInsert​(java.lang.String data,
                                         int n,
                                         char insert)
        Inserts a char into a string after every n chars. Can for example be used to add a linebreak every n characters
        Parameters:
        data - to insert the chars into
        n - offset between inserts
        insert - char
        Returns:
        byte array with inserted chars
      • splitStringEvery

        public static java.lang.String[] splitStringEvery​(java.lang.String s,
                                                          int interval)
        splits a given string into parts of maximum length of interval
        Parameters:
        s - to split
        interval - amount of chars to split the string after
        Returns:
        array of strings representing splitted input
      • getFileExtension

        public static java.lang.String getFileExtension​(java.io.File file)
        Returns the file extension of a file. If file has no extension an empty String is returned
        Parameters:
        file - to get the extension of
        Returns:
        the files extension
      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] bytes)
        Converts a byte array to hexadecimal represented String
        Parameters:
        bytes - to transform
        Returns:
        hexadecimal representation
      • hexStringToByteArray

        public static byte[] hexStringToByteArray​(java.lang.String string)
        Converts a hexadecimal string to byte array
        Parameters:
        string - to transform
        Returns:
        byte representation
      • hexToString

        public static java.lang.String hexToString​(java.lang.String hexStr)
        Decodes a hexadecimal string
        Parameters:
        hexStr - hexadecimal string to be decoded
        Returns:
        decoded string