Class StringUtils

java.lang.Object
dev.netcode.util.StringUtils

public class StringUtils extends Object
This class contains functions to generate, manipulate or use Strings
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • padLeft

      public static String padLeft(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 String padRight(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 String decodeURL(String encoded)
      Decodes escaped special chars from URLs like '%20' to their UTF-8 counterpart
      Parameters:
      encoded - URL
      Returns:
      decoded URL
    • applySha256

      public static String applySha256(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(File file, int fileLength) throws IOException
      Reads file into byte array
      Parameters:
      file - to be read
      fileLength - of the file
      Returns:
      byte contents of the file
      Throws:
      IOException - if something fails
    • saveToFile

      public static boolean saveToFile(Path file, 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 HashMap<String,​String> stringToMap(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(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 String[] splitStringEvery(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 String getFileExtension(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