Class StringUtilities
Various static methods helpful for comparing and manipulating strings.
-
Method Summary
Modifier and TypeMethodDescriptionstatic final int
Compare strings based on their integer value of they are both integers, otherwise their lexicographic order.static final int
Compare strings based on the lexicographic order of their values, but accounting for non-zero padded integers.static final boolean
Does a string contain any one of an array of strings ?static final boolean
containsRegardlessOfCase
(String string, String[] substrings) Does a string contain any one of an array of strings regardless of case ?static String
dump
(char[] chars) Create a dump of the decimal offset, hexadecimal values and printable string values of an array of char.static String
Create a dump of the decimal offset, hexadecimal values and printable string values of a String.static final Vector
getDelimitedValues
(String string, String delimiter) Get delimited values from a string.static String
getStringNoLongerThanTruncatedIfNecessary
(String str, int wanted) static String
padPositiveInteger
(String str, int length, Character pad) static final String
removeLeadingCharacter
(String src, char rmchar) Remove any leading instances of a particular character from a string.static final String
Remove any leading or trailing padding from a string.static final String
Remove any leading spaces from a string.static final String
Remove any leading instances of whitespace or control characters from a string.static final String
removeTrailingCharacter
(String src, char rmchar) Remove any trailing instances of a particular character from a string.static final String
Remove any trailing spaces from a string.static final String
Remove any trailing instances of whitespace or control characters from a string.static final String
replaceAllInWith
(String string, String oldChars, String newChars) Replace all listed characters in a string with those listed as replacements.static String
toString
(double[] doubleArray) static String
static String
static final String
toUpperCamelCase
(String string) static String
zeroPadPositiveInteger
(String str, int length)
-
Method Details
-
toUpperCamelCase
-
replaceAllInWith
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 withinoldChars
- a String containing characters to be replacednewChars
- a String containing corresponding characters to use as replacements (may be null)- Returns:
- a String with the characters replaced
-
contains
Does a string contain any one of an array of strings ?
- Parameters:
string
- the string to testsubstrings
- an array of strings to look for within string- Returns:
- true if any string in substrings is found within string
-
containsRegardlessOfCase
Does a string contain any one of an array of strings regardless of case ?
- Parameters:
string
- the string to testsubstrings
- an array of strings to look for within string- Returns:
- true if any string in substrings is found within string
-
getDelimitedValues
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 valuesdelimiter
- the delimiter- Returns:
- a Vector of String values
-
removeTrailingCharacter
Remove any trailing instances of a particular character from a string.
- Parameters:
src
- the string that may have trailing charactersrmchar
- 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
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
Remove any leading instances of a particular character from a string.
- Parameters:
src
- the string that may have leading charactersrmchar
- 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
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
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
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
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
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 compareds2
- 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
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 compareds2
- 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
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
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
-
toString
-
toString
-
padPositiveInteger
-
zeroPadPositiveInteger
-
getStringNoLongerThanTruncatedIfNecessary
-