- java.lang.Object
-
- dev.netcode.util.StringUtils
-
public class StringUtils extends java.lang.ObjectThis 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.StringapplySha256(java.lang.String input)Applies the SHA-256 algorithm to the given input.static java.lang.StringbytesToHex(byte[] bytes)Converts a byte array to hexadecimal represented Stringstatic byte[]chunkInsert(java.lang.String data, int n, char insert)Inserts a char into a string after every n chars.static java.lang.StringdecodeURL(java.lang.String encoded)Decodes escaped special chars from URLs like '%20' to their UTF-8 counterpartstatic java.lang.StringgetFileExtension(java.io.File file)Returns the file extension of a file.static byte[]hexStringToByteArray(java.lang.String string)Converts a hexadecimal string to byte arraystatic java.lang.StringhexToString(java.lang.String hexStr)Decodes a hexadecimal stringstatic java.lang.StringpadLeft(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.StringpadRight(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 arraystatic booleansaveToFile(java.nio.file.Path file, java.lang.String data)Saves a string to a filestatic java.lang.String[]splitStringEvery(java.lang.String s, int interval)splits a given string into parts of maximum length of intervalstatic 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 aHashMap.
-
-
-
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 padtotalLength- 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 padtotalLength- 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.IOExceptionReads file into byte array- Parameters:
file- to be readfileLength- 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 indata- 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 aHashMap.- Parameters:
input- string- Returns:
HashMaprepresenting 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 inton- offset between insertsinsert- 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 splitinterval- 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
-
-