Class StringExtensions


  • public final class StringExtensions
    extends java.lang.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 Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String addDoubleQuotationMarks​(java.lang.String s)
      Puts to the given String at the start and end quotes.
      static java.lang.String addSingleQuotationMarks​(java.lang.String s)
      Puts to the given String at the start and end quotes.
      static byte[] convertToBytearray​(char[] source)
      Converts the given chararray to a bytearray.
      static char[] convertToCharArray​(byte[] source)
      Convert the given byte array to char array.
      static java.lang.Character convertUnicodeStringToCharacter​(java.lang.String theUnicodeString)
      Converts the given unicode String object to character object.
      static java.util.List<java.lang.String> filterStringsWithSeperator​(java.util.List<java.lang.String> ignoreFieldNames, java.lang.String separator)
      Filter the given List of String objects with the given separator.
      static java.lang.String firstCharacterToLowerCase​(java.lang.String fieldName)
      Sets the first character from the given string to lower case and returns it.
      static java.lang.String firstCharacterToUpperCase​(java.lang.String fieldName)
      Sets the first character from the given string to upper case and returns it.
      static java.lang.String getAttributName​(java.lang.String name)
      Gets the Attribut-name without brackets.
      static java.lang.String getIndex​(java.lang.String name)
      Gets the first index from the brackets.
      static java.lang.String getItemNumber​(java.lang.String name)
      Gets the ItemNumber from the String name.
      static java.lang.String getStringAfterUnderscore​(java.lang.String element)
      The Method getStringAfterUnderscore(String) gets the substring after the first underscore.
      static java.lang.String getStringBeforeUnderscore​(java.lang.String element)
      The Method getStringBeforeUnderscore(String) gets the substring before the first underscore.
      static java.lang.String getValue​(java.util.Map<java.lang.String,​java.lang.String> data, java.lang.String key, java.lang.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.
      static boolean isNullOrEmpty​(java.lang.String str)
      The method isNullOrEmpty(String) checks if the given String is empty or null.
      static boolean isNumber​(java.lang.String testString)
      Checks if the given String is an Number.
      static java.lang.String lastCharacter​(java.lang.String str)
      Gets the last character of the given String object.
      static java.lang.String lastCharacterToUpperCase​(java.lang.String fieldName)
      Sets the last character from the given string to upper case and returns it.
      static java.lang.String readLine​(java.lang.String stringWithCarriageReturns)
      Reads the given String till it finds a carriage return and returns it.
      static java.lang.String[] removeEmptyString​(java.lang.String[] words)
      Removes empty Strings from the given string array and gives it back.
      static java.lang.String removeFirstAndLastCharacter​(java.lang.String s)
      Removes the first and the last character from the given String
      static java.lang.String removeNewlineCharacters​(java.lang.String subject)
      Removes the newline characters from the given String.
      static java.lang.String removeQuotationMarks​(java.lang.String s)
      Deprecated.
      use instead the method removeFirstAndLastCharacter
      Note: will be removed in next minor release
      static java.lang.String replaceAll​(java.lang.String original, java.lang.String[] findString, java.lang.String replaceWith)
      Replaces all occurrences of a String within another String.
      static java.lang.String replaceAll​(java.lang.String original, java.lang.String findString, java.lang.String replaceWith)
      Replaces all occurrences of a String within another String.
      static java.lang.String replaceEach​(java.lang.String input, java.lang.String searchRegexPattern, java.lang.String replace)
      Replace each occurences from the search pattern(regex) with the given replace String of the given input String.
      static java.lang.String replaceLast​(java.lang.String original, java.lang.String findString, java.lang.String replacement)
      Replaces the last occurrence of a String within another String.
      static java.util.List<java.lang.String> splitByFixedLength​(java.lang.String input, int fixedLength)
      Split the given String in parts in the given fixed length.
      static int[] toIntegerArray​(java.lang.String integerArrayAsString, java.lang.String delimiter)
      Transforms the given String that holds successive numbers delimited with a delimiter into an integer array.
      static <T> java.lang.String toString​(T object)
      Prints the Object.toString() and if the given object is null a corresponding information.
      static java.lang.String toUnicode​(java.lang.String toUnicode, boolean toLowerCase)
      Converts all characters from the given String to unicodes characters encoded like \uxxxx.
      static java.lang.String toUnicodeChars​(java.lang.String s, boolean toLowerCase)
      Converts all characters from the given String to unicodes characters encoded like \uxxxx.
      • Methods inherited from class java.lang.Object

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

      • addDoubleQuotationMarks

        public static final java.lang.String addDoubleQuotationMarks​(java.lang.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 final java.lang.String addSingleQuotationMarks​(java.lang.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 final 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 java.lang.Character convertUnicodeStringToCharacter​(java.lang.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 the unicode string
        Returns:
        the converted character object.
      • filterStringsWithSeperator

        public static java.util.List<java.lang.String> filterStringsWithSeperator​(java.util.List<java.lang.String> ignoreFieldNames,
                                                                                  java.lang.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 final java.lang.String firstCharacterToLowerCase​(java.lang.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 final java.lang.String firstCharacterToUpperCase​(java.lang.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 final java.lang.String getAttributName​(java.lang.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 final java.lang.String getIndex​(java.lang.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 final java.lang.String getItemNumber​(java.lang.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 final java.lang.String getStringAfterUnderscore​(java.lang.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 final java.lang.String getStringBeforeUnderscore​(java.lang.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 java.lang.String getValue​(java.util.Map<java.lang.String,​java.lang.String> data,
                                                java.lang.String key,
                                                java.lang.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 final boolean isNullOrEmpty​(java.lang.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 final boolean isNumber​(java.lang.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 java.lang.String lastCharacter​(java.lang.String str)
        Gets the last character of the given String object.
        Parameters:
        str - the str
        Returns:
        the last character
      • lastCharacterToUpperCase

        public static final java.lang.String lastCharacterToUpperCase​(java.lang.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 final java.lang.String readLine​(java.lang.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 java.lang.String[] removeEmptyString​(java.lang.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 final java.lang.String removeFirstAndLastCharacter​(java.lang.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 java.lang.String removeNewlineCharacters​(java.lang.String subject)
        Removes the newline characters from the given String.
        Parameters:
        subject - the subject
        Returns:
        the string
      • removeQuotationMarks

        @Deprecated
        public static final java.lang.String removeQuotationMarks​(java.lang.String s)
        Deprecated.
        use instead the method removeFirstAndLastCharacter
        Note: will be removed in next minor release
        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 final java.lang.String replaceAll​(java.lang.String original,
                                                        java.lang.String findString,
                                                        java.lang.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 final java.lang.String replaceAll​(java.lang.String original,
                                                        java.lang.String[] findString,
                                                        java.lang.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.
      • replaceEach

        public static java.lang.String replaceEach​(java.lang.String input,
                                                   java.lang.String searchRegexPattern,
                                                   java.lang.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 final java.lang.String replaceLast​(java.lang.String original,
                                                         java.lang.String findString,
                                                         java.lang.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 java.util.List<java.lang.String> splitByFixedLength​(java.lang.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​(java.lang.String integerArrayAsString,
                                           java.lang.String delimiter)
                                    throws java.lang.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:
        java.lang.NumberFormatException - is thrown if the given String is not valid.
      • toString

        public static <T> java.lang.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 java.lang.String toUnicode​(java.lang.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 java.lang.String toUnicodeChars​(java.lang.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.