Class StringExtensions

java.lang.Object
io.github.astrapi69.string.StringExtensions

public final class StringExtensions extends Object
The class StringExtensions provides methods for manipulate string objects.

Note: As the String class is immutable not the given String is manipulated, a new String object is created with the manipulation.
  • Method Details

    • addDoubleQuotationMarks

      public static String addDoubleQuotationMarks(String s)
      Puts to the given String at the start and end quotes.
      Parameters:
      s - The String to handle.
      Returns:
      The String with quotes.
    • addSingleQuotationMarks

      public static String addSingleQuotationMarks(String s)
      Puts to the given String at the start and end quotes.
      Parameters:
      s - The String to handle.
      Returns:
      The String with quotes.
    • convertToBytearray

      public static byte[] convertToBytearray(char[] source)
      Converts the given chararray to a bytearray.
      Parameters:
      source - the source.
      Returns:
      the byte[]
    • convertToCharArray

      public static char[] convertToCharArray(byte[] source)
      Convert the given byte array to char array.
      Parameters:
      source - the source.
      Returns:
      the char[]
    • convertUnicodeStringToCharacter

      public static Character convertUnicodeStringToCharacter(String theUnicodeString)
      Converts the given unicode String object to character object.
      Example: the given unicode String object is "\u03A9"
      the return character object would be "Ω".
      Parameters:
      theUnicodeString - the unicode string
      Returns:
      the converted character object.
    • filterStringsWithSeperator

      public static List<String> filterStringsWithSeperator(List<String> ignoreFieldNames, String separator)
      Filter the given List of String objects with the given separator.
      Parameters:
      ignoreFieldNames - the ignore field names
      separator - the separator
      Returns:
      the filtered List
    • firstCharacterToLowerCase

      public static String firstCharacterToLowerCase(String fieldName)
      Sets the first character from the given string to lower case and returns it. Example:
      Given fieldName: UserName
      Result: userName
      Parameters:
      fieldName - The String to modify.
      Returns:
      The modified string.
    • firstCharacterToUpperCase

      public static String firstCharacterToUpperCase(String fieldName)
      Sets the first character from the given string to upper case and returns it. Example:
      Given fieldName: userName
      Result: UserName
      Parameters:
      fieldName - The String to modify.
      Returns:
      The modified string.
    • getAttributName

      public static String getAttributName(String name)
      Gets the Attribut-name without brackets. For example: If "indio[5][1]" is the given String than the name is "indio" and the first index is "5" and the second is "1". Than the Method returns "indio".
      Parameters:
      name - The name with brackets.
      Returns:
      The name without brackets.
    • getIndex

      public static String getIndex(String name)
      Gets the first index from the brackets. For example: If "indio[5][1]" is the given String than the name is "indio" and the first index is "5" and the second is "1". Than the Method returns "5".
      Parameters:
      name - The name from the Attribute
      Returns:
      the index from the Attribute
    • getItemNumber

      public static String getItemNumber(String name)
      Gets the ItemNumber from the String name. For example: If "indio[5][1]" is the given String than the name is "indio" and the first index is "5" and the second is "1". Than the Method returns "1".
      Parameters:
      name - the name
      Returns:
      the ItemNumber
    • getStringAfterUnderscore

      public static String getStringAfterUnderscore(String element)
      The Method getStringAfterUnderscore(String) gets the substring after the first underscore. For example:Assume that element=foobar_value then the result string is "value".
      Parameters:
      element - The element to get the substing.
      Returns:
      The result String.
    • getStringBeforeUnderscore

      public static String getStringBeforeUnderscore(String element)
      The Method getStringBeforeUnderscore(String) gets the substring before the first underscore. For example:Assume that element=foobar_value then the result string is "foobar".
      Parameters:
      element - The element to get the substing.
      Returns:
      The result String.
    • getValue

      public static String getValue(Map<String,String> data, String key, String defaultValue)
      Gets the value from the given map and if it does not exist or is empty the given default value will be returned.
      Parameters:
      data - the data
      key - the key
      defaultValue - the default value
      Returns:
      the value
    • isNullOrEmpty

      public static boolean isNullOrEmpty(String str)
      The method isNullOrEmpty(String) checks if the given String is empty or null.
      Parameters:
      str - The String to check.
      Returns:
      true if the given String is empty or null otherwise false.
    • isNumber

      public static boolean isNumber(String testString)
      Checks if the given String is an Number.
      Parameters:
      testString - The String to check.
      Returns:
      true if the given String is a number otherwise false.
    • lastCharacter

      public static String lastCharacter(String str)
      Gets the last character of the given String object.
      Parameters:
      str - the str
      Returns:
      the last character
    • lastCharacterToUpperCase

      public static String lastCharacterToUpperCase(String fieldName)
      Sets the last character from the given string to upper case and returns it. Example:
      Given fieldName: userName
      Result: userNamE
      Parameters:
      fieldName - The String to modify.
      Returns:
      The modified string.
    • readLine

      public static String readLine(String stringWithCarriageReturns)
      Reads the given String till it finds a carriage return and returns it.
      Parameters:
      stringWithCarriageReturns - The String.
      Returns:
      Return the String till the carriage return.
    • removeEmptyString

      public static String[] removeEmptyString(String[] words)
      Removes empty Strings from the given string array and gives it back. For instance if you use the String.split("\\W") method you get all the words from the given String as an string array. But this array contains empty Strings too. This method removes this empty strings from that array.
      Parameters:
      words - The string array to remove empty strings.
      Returns:
      An string array without empty Strings.
    • removeFirstAndLastCharacter

      public static String removeFirstAndLastCharacter(String s)
      Removes the first and the last character from the given String
      Parameters:
      s - The String to handle
      Returns:
      The resulted String
    • removeNewlineCharacters

      public static String removeNewlineCharacters(String subject)
      Removes the newline characters from the given String.
      Parameters:
      subject - the subject
      Returns:
      the string
    • removeQuotationMarks

      public static String removeQuotationMarks(String s)
      Removes from the given String at the start and end quotes.
      Parameters:
      s - The String to handle.
      Returns:
      The String without quotes.
    • replaceAll

      public static String replaceAll(String original, String findString, String replaceWith)
      Replaces all occurrences of a String within another String.
      Parameters:
      original - The String to search.
      findString - The search pattern.
      replaceWith - Replace with that String.
      Returns:
      Returns a new String with the replaced Strings.
    • replaceAll

      public static String replaceAll(String original, String[] findString, String replaceWith)
      Replaces all occurrences of a String within another String.
      Parameters:
      original - The String to search.
      findString - An array with search patterns.
      replaceWith - Replace with that String.
      Returns:
      Returns a new String with the replaced Strings.
    • replaceAll

      public static String replaceAll(String original, Map<String,String> replaceWith)
      Replaces all occurrences of a String within another String.
      Parameters:
      original - The String to search.
      replaceWith - Replace map with the specific replace values
      Returns:
      Returns a new String with the replaced Strings.
    • replaceEach

      public static String replaceEach(String input, String searchRegexPattern, String replace)
      Replace each occurences from the search pattern(regex) with the given replace String of the given input String.
      Parameters:
      input - the input String that will be changed.
      searchRegexPattern - the search regex pattern
      replace - the String to replace with.
      Returns:
      the resulted string
    • replaceLast

      public static String replaceLast(String original, String findString, String replacement)
      Replaces the last occurrence of a String within another String.
      Parameters:
      original - The String to search.
      findString - The search pattern.
      replacement - Replace with that String.
      Returns:
      Returns a new String with the replaced Strings.
    • splitByFixedLength

      public static List<String> splitByFixedLength(String input, int fixedLength)
      Split the given String in parts in the given fixed length.
      Parameters:
      input - the input
      fixedLength - the fixed length
      Returns:
      the list with the splitted String objects
    • toIntegerArray

      public static int[] toIntegerArray(String integerArrayAsString, String delimiter) throws NumberFormatException
      Transforms the given String that holds successive numbers delimited with a delimiter into an integer array. Note: A valid String object holds only integer values delimited with the given delimiter. Example: A valid String object is "12, 13, 14, 16, 17, 18" that results in an integer array of [12, 13, 14, 16, 17, 18]
      Parameters:
      integerArrayAsString - the integer array as string
      delimiter - the delimiter
      Returns:
      the created integer array from the given String
      Throws:
      NumberFormatException - is thrown if the given String is not valid.
    • toString

      public static <T> String toString(T object)
      Prints the Object.toString() and if the given object is null a corresponding information.
      Type Parameters:
      T - the generic type
      Parameters:
      object - the object
      Returns:
      the string
    • toUnicode

      public static String toUnicode(String toUnicode, boolean toLowerCase)
      Converts all characters from the given String to unicodes characters encoded like \uxxxx.
      Parameters:
      toUnicode - The String to convert.
      toLowerCase - If true the letters from the unicode characters are lower case.
      Returns:
      The converted String.
    • toUnicodeChars

      public static String toUnicodeChars(String s, boolean toLowerCase)
      Converts all characters from the given String to unicodes characters encoded like \uxxxx.
      Parameters:
      s - The String to convert.
      toLowerCase - If true the letters from the unicode characters are lower case.
      Returns:
      The converted String.
    • newCharacterList

      public static List<Character> newCharacterList(String input)
      Factory method for create new ArrayList of unique characters from the given text
      Parameters:
      input - the input text
      Returns:
      the new List with the unique characters
    • newCharacterList

      public static List<Character> newCharacterList(String text, Comparator<Character> comparator)
      Factory method for create new ArrayList of unique characters from the given text sorted with the given Comparator

      Note: This method can be used for a custom Comparator that have a defined order. For example: // defined custom order List<Character> definedOrder = Arrays.asList('c', 'b', 'a', 'd', '.', ...); Comparator<Character> customComparator = Comparator.comparing(character -> definedOrder.indexOf(character));
      Parameters:
      text - the text
      comparator - the comparator
      Returns:
      the new List with the unique characters
    • toString

      public static String toString(List<Character> characterList)
      Converts the given character list to a String
      Parameters:
      characterList - the list of characters to convert
      Returns:
      the new String from the given character list