Class StringUtilities


  • public class StringUtilities
    extends java.lang.Object

    Various static methods helpful for comparing and manipulating strings.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int compareStringsThatMayBeIntegers​(java.lang.String s1, java.lang.String s2)
      Compare strings based on their integer value of they are both integers, otherwise their lexicographic order.
      static int compareStringsWithEmbeddedNonZeroPaddedIntegers​(java.lang.String s1, java.lang.String s2)
      Compare strings based on the lexicographic order of their values, but accounting for non-zero padded integers.
      static boolean contains​(java.lang.String string, java.lang.String[] substrings)
      Does a string contain any one of an array of strings ?
      static boolean containsRegardlessOfCase​(java.lang.String string, java.lang.String[] substrings)
      Does a string contain any one of an array of strings regardless of case ?
      static java.lang.String dump​(char[] chars)
      Create a dump of the decimal offset, hexadecimal values and printable string values of an array of char.
      static java.lang.String dump​(java.lang.String s)
      Create a dump of the decimal offset, hexadecimal values and printable string values of a String.
      static java.util.Vector getDelimitedValues​(java.lang.String string, java.lang.String delimiter)
      Get delimited values from a string.
      static java.lang.String getStringNoLongerThanTruncatedIfNecessary​(java.lang.String str, int wanted)  
      static java.lang.String padPositiveInteger​(java.lang.String str, int length, java.lang.Character pad)  
      static java.lang.String removeLeadingCharacter​(java.lang.String src, char rmchar)
      Remove any leading instances of a particular character from a string.
      static java.lang.String removeLeadingOrTrailingWhitespaceOrISOControl​(java.lang.String src)
      Remove any leading or trailing padding from a string.
      static java.lang.String removeLeadingSpaces​(java.lang.String src)
      Remove any leading spaces from a string.
      static java.lang.String removeLeadingWhitespaceOrISOControl​(java.lang.String src)
      Remove any leading instances of whitespace or control characters from a string.
      static java.lang.String removeTrailingCharacter​(java.lang.String src, char rmchar)
      Remove any trailing instances of a particular character from a string.
      static java.lang.String removeTrailingSpaces​(java.lang.String src)
      Remove any trailing spaces from a string.
      static java.lang.String removeTrailingWhitespaceOrISOControl​(java.lang.String src)
      Remove any trailing instances of whitespace or control characters from a string.
      static java.lang.String replaceAllInWith​(java.lang.String string, java.lang.String oldChars, java.lang.String newChars)
      Replace all listed characters in a string with those listed as replacements.
      static java.lang.String toString​(double[] doubleArray)  
      static java.lang.String toString​(java.lang.String[] stringArray)  
      static java.lang.String toString​(java.lang.String[][] stringArrays)  
      static java.lang.String toUpperCamelCase​(java.lang.String string)  
      static java.lang.String zeroPadPositiveInteger​(java.lang.String str, int length)  
      • Methods inherited from class java.lang.Object

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

      • toUpperCamelCase

        public static final java.lang.String toUpperCamelCase​(java.lang.String string)
      • replaceAllInWith

        public static final java.lang.String replaceAllInWith​(java.lang.String string,
                                                              java.lang.String oldChars,
                                                              java.lang.String newChars)

        Replace all listed characters in a string with those listed as replacements.

        If newchars is null or shorter than oldchars, the character will be deleted.

        Parameters:
        string - the String to replace characters within
        oldChars - a String containing characters to be replaced
        newChars - a String containing corresponding characters to use as replacements (may be null)
        Returns:
        a String with the characters replaced
      • contains

        public static final boolean contains​(java.lang.String string,
                                             java.lang.String[] substrings)

        Does a string contain any one of an array of strings ?

        Parameters:
        string - the string to test
        substrings - an array of strings to look for within string
        Returns:
        true if any string in substrings is found within string
      • containsRegardlessOfCase

        public static final boolean containsRegardlessOfCase​(java.lang.String string,
                                                             java.lang.String[] substrings)

        Does a string contain any one of an array of strings regardless of case ?

        Parameters:
        string - the string to test
        substrings - an array of strings to look for within string
        Returns:
        true if any string in substrings is found within string
      • getDelimitedValues

        public static final java.util.Vector getDelimitedValues​(java.lang.String string,
                                                                java.lang.String delimiter)

        Get delimited values from a string.

        Consecutive delimiters result in an empty (zero length not null) string value.

        Hence always returns a Vector one longer than the number of delimiters present.

        Parameters:
        string - the string containing the delimited values
        delimiter - the delimiter
        Returns:
        a Vector of String values
      • removeTrailingCharacter

        public static final java.lang.String removeTrailingCharacter​(java.lang.String src,
                                                                     char rmchar)

        Remove any trailing instances of a particular character from a string.

        Parameters:
        src - the string that may have trailing characters
        rmchar - the character, all trailing instances of which are to be removed
        Returns:
        the value of the string argument with any instances of the trailing character removed
      • removeTrailingWhitespaceOrISOControl

        public static final java.lang.String removeTrailingWhitespaceOrISOControl​(java.lang.String src)

        Remove any trailing instances of whitespace or control characters from a string.

        Parameters:
        src - the string that may have trailing characters
        Returns:
        the value of the string argument with any instances of trailing whitespace or control characters removed
      • removeLeadingCharacter

        public static final java.lang.String removeLeadingCharacter​(java.lang.String src,
                                                                    char rmchar)

        Remove any leading instances of a particular character from a string.

        Parameters:
        src - the string that may have leading characters
        rmchar - the character, all leading instances of which are to be removed
        Returns:
        the value of the string argument with any instances of the leading character removed
      • removeLeadingWhitespaceOrISOControl

        public static final java.lang.String removeLeadingWhitespaceOrISOControl​(java.lang.String src)

        Remove any leading instances of whitespace or control characters from a string.

        Parameters:
        src - the string that may have trailing characters
        Returns:
        the value of the string argument with any instances of trailing whitespace or control characters removed
      • removeTrailingSpaces

        public static final java.lang.String removeTrailingSpaces​(java.lang.String src)

        Remove any trailing spaces from a string.

        Parameters:
        src - the string that may have trailing spaces
        Returns:
        the value of the string argument with any trailing spaces removed
      • removeLeadingSpaces

        public static final java.lang.String removeLeadingSpaces​(java.lang.String src)

        Remove any leading spaces from a string.

        Parameters:
        src - the string that may have leading spaces
        Returns:
        the value of the string argument with any leading spaces removed
      • removeLeadingOrTrailingWhitespaceOrISOControl

        public static final java.lang.String removeLeadingOrTrailingWhitespaceOrISOControl​(java.lang.String src)

        Remove any leading or trailing padding from a string.

        Padding in this context means leading or trailing white space of any kind or null characters.

        Parameters:
        src - the string that may have padding
        Returns:
        the value of the string argument with any padding removed
      • compareStringsThatMayBeIntegers

        public static final int compareStringsThatMayBeIntegers​(java.lang.String s1,
                                                                java.lang.String s2)

        Compare strings based on their integer value of they are both integers, otherwise their lexicographic order.

        For example, "001" and"1" would be treated as equal, whilst "1" would be considered as occuring before "10", which would not be the case with a simple lexicographic ordering.

        Parameters:
        s1 - the first of two strings to be compared
        s2 - the first of two strings to be compared
        Returns:
        the value 0 if the first string is equal to the second string; a value less than 0 if the first string is less than the second string; and a value greater than 0 if the first string is greater than the second string
      • compareStringsWithEmbeddedNonZeroPaddedIntegers

        public static final int compareStringsWithEmbeddedNonZeroPaddedIntegers​(java.lang.String s1,
                                                                                java.lang.String s2)

        Compare strings based on the lexicographic order of their values, but accounting for non-zero padded integers.

        Note that the comparison is more complex than a simple lexicographic comparison of strings (as described in the definition of java.lang.String.compareTo(String) but rather accounts for embedded non-zero padded integers by treating occurrences of space delimited integers as integer values rather than strings. For example, "a 001 b" and"a 1 b" would be treated as equal, whilst "a 1 b" would be considered as occuring before "a 10 b", which would not be the case with a simple lexicographic ordering.

        Parameters:
        s1 - the first of two strings to be compared
        s2 - the first of two strings to be compared
        Returns:
        the value 0 if the first string is equal to the second string; a value less than 0 if the first string is lexicographically less than the second string; and a value greater than 0 if the first string is lexicographically greater than the second string
      • dump

        public static java.lang.String dump​(java.lang.String s)

        Create a dump of the decimal offset, hexadecimal values and printable string values of a String.

        Parameters:
        s - the String to be dumped as if it were an array of char
        Returns:
        a string containing the multiline result
      • dump

        public static java.lang.String dump​(char[] chars)

        Create a dump of the decimal offset, hexadecimal values and printable string values of an array of char.

        Parameters:
        chars - the array of char to be dumped
        Returns:
        a string containing the multiline result
      • toString

        public static java.lang.String toString​(double[] doubleArray)
      • toString

        public static java.lang.String toString​(java.lang.String[] stringArray)
      • toString

        public static java.lang.String toString​(java.lang.String[][] stringArrays)
      • padPositiveInteger

        public static java.lang.String padPositiveInteger​(java.lang.String str,
                                                          int length,
                                                          java.lang.Character pad)
      • zeroPadPositiveInteger

        public static java.lang.String zeroPadPositiveInteger​(java.lang.String str,
                                                              int length)
      • getStringNoLongerThanTruncatedIfNecessary

        public static java.lang.String getStringNoLongerThanTruncatedIfNecessary​(java.lang.String str,
                                                                                 int wanted)