Package com.globalmentor.java
Class Strings
java.lang.Object
com.globalmentor.java.Strings
Various text manipulating functions. These methods work on
String
objects, which are immutable heavyweight objects that must be recreated with every
modification. Many of these methods should therefore be avoided in favor of their corresponding StringBuilders
methods, which operate on
StringBuilder
objects.- Author:
- Garret Wilson
- See Also:
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
asNonEmptyString
(String string) Returns the string ornull
if the string is the empty string ("").static String
collapse
(String inString, Characters collapseChars, String replaceString) Collapses every run of any number of collapseChars to a single replacement string.static String[]
createArray
(String string) Creates an array of strings from the given string.static String
createString
(char firstChar, char lastChar) Creates a string containing a range of characters.static String
createString
(char ch, int count) Creates a string with a given repetition of characters.static final boolean
equalsIgnoreCase
(String string1, String string2) Compares two strings to make sure that the strings are equal without regard to case, or that the strings are both set tonull
.static String
forceLength
(String inString, int len, char ch) Ensures that the given string is the correct length by adding or deleting characters to or from the end.static String
forceLength
(String inString, int len, char ch, int pos) Ensures that the given string is the correct length by adding or deleting characters to or from the requested position.static byte[]
getASCIIZBytes
(String string, int length) Creates an array of bytes of the specified length and stores the bytes of the string, in UTF-8, in the array appended with a zero byte.static byte[]
getASCIIZBytes
(String string, int length, Charset charset) Creates an array of bytes of the specified length and stores the bytes of the string, in the array appended with a zero byte.static InputStream
getInputStream
(String string, String encoding) Creates an input stream from which to read the given string.static int
getWordBeginning
(String inString, int index) Returns the beginning of the word at index.static int
getWordEnd
(String inString, int index) Returns the end of the word at index.static int
indexOfIgnoreCase
(String string, char c) Finds the index of a particular character, ignoring case.static int
indexOfIgnoreCase
(String string, char c, int fromIndex) Finds the index of a particular character, ignoring case, from a given index.static int
indexOfIgnoreCase
(String string, String substring) Finds the index of a particular substring, ignoring case.static int
indexOfIgnoreCase
(String string, String substring, int fromIndex) Finds the index of a particular substring, ignoring case, from a given index.static String
Inserts a character at a specified index.static String
Inserts a string at a specified index.static int
lastIndexOfIgnoreCase
(String string, String substring, int fromIndex) Searches a string in reverse and returns the last index of a substring without case sensitivity, starting fromfromIndex
.static String
makeHTMLHyperlinks
(String inString) Parses the string and finds any hyperlinks, to which it then creates and inserts the appropriate HTML tags.static String
makeStringLength
(String inString, int len, char ch) Deprecated.static String
makeStringLength
(String inString, int len, char ch, int pos) Deprecated.to be removed in favor offorceLength(String, int, char, int)
.static String
Removes a character at the specified index.static String
Removes several characters at the specified index.static String
removeAfterFirstChar
(String string, Characters delimiters) Removes all characters that come after and including the first occurrence of a character in the given delimiter string.static String
removeBeforeLast
(String string, char c) Removes all characters that come before the last occurrence of the given character.static String
removeLengthIgnoreCase
(String string, String substring) Removes the given substring, matched without case sensitivity, and everything following the string.static String
Replaces each matching character with the corresponding replacement string.static String
Replaces every occurrence of a specified character with a string.static String
Removes several characters at the specified index and replaces them with the given string.static String
replace
(String inString, Characters matchCharacters, char replacementChar) Replaces any of several matching characters with a particular character.static String
replace
(String inString, Characters matchCharacters, String replacementString) Replaces any of several matching characters with a particular string.static String
Replaces every occurrence of a specified string with another string.static boolean
startsWithIgnoreCase
(String string, String prefix) Determines whether the string starts with the given prefix, ignoring case.static final String
stringOf
(char... chars) Convenience method to create a string from characters using varargs.static String
stringToken
(String inString, int tokenNumber, Characters delimiters) Returns the specified numbered (one-based) token in the specified string, separated by delimiters.static String
stringWord
(String inString, int wordNumber) Returns the specified numbered (one-based) word in the specified string.static int
tokenEndIndex
(String inString, int tokenNumber, Characters delimiters) Returns the index right after the given numbered (one-based) token.static int
tokenIndex
(String inString, int tokenNumber, Characters delimiters) Returns the index of the given numbered (one-based) token.static String
trim
(String inString, Characters delimiters) Trims the specified delimiters from the beginning and end of the string.static String
trimBeginning
(String inString, char removeChar, int occurrence) Trims the left side of the string beginning at the specified occurrence of removeChar from the beginning.static String
trimBeginning
(String inString, String beginString) If the input string begins with the specifid string, trims that string.static String
trimBeginningFirst
(String inString, char removeChar) Trims the left side of the string beginning at the first occurrence of removeChar.static String
trimBeginningLast
(String inString, char removeChar) Trims the left side of the string beginning at the last occurrence of removeChar.static String
Trims the right side of the string beginning at the specified occurrence of removeChar from the end.static String
If the input string ends with the specified string, trims that string.static String
trimEndFirst
(String inString, char removeChar) Trims the right side of the string beginning at the first occurrence of removeChar.static String
trimEndLast
(String inString, char removeChar) Trims the right side of the string beginning at the last occurrence of removeChar.static String
trimWhitespaceNoBreak
(String inString) Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning and end of the string.static String
trimWhitespaceNoBreakBeginning
(String inString) Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning of the string.static String
trimWhitespaceNoBreakEnd
(String inString) Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning of the string.static String
Truncates the string, if needed, to ensure that the string is no longer than the provided length.static String
truncateChar
(String string, Characters delimiters) Removes everything after and including the first occurrence of one of the given characters.static int
wordEndIndex
(String inString, int wordNumber) Returns the index right after the given numbered (one-based) word.static int
Returns the index of the given numbered (one-based) word.static String
Performs word-wrapping on the input string by strategically inserting ends-of-line, ensuring that each line is no longer than the specified length.static String
Performs word-wrapping on the input string by strategically inserting ends-of-line, ensuring that each line is no longer than the specified length.static <T> String
Writes an object to a string using the given I/O support, converting bytes to a string using the UTF-8 charset.static <T> String
Writes an object to a string using the given I/O support.
-
Field Details
-
NO_STRINGS
A shared empty array of strings.
-
-
Method Details
-
asNonEmptyString
Returns the string ornull
if the string is the empty string ("").- Parameters:
string
- The string to examine.- Returns:
string
if the length of the string is greater than zero, otherwisenull
.
-
stringOf
Convenience method to create a string from characters using varargs.- Parameters:
chars
- The characters that should make up the string.- Returns:
- A string created from the given characters.
- Throws:
NullPointerException
- if the given chars isnull
.
-
createArray
Creates an array of strings from the given string.- Parameters:
string
- The string to include in the array, ornull
if the array should be empty.- Returns:
- A non-
null
array containing the string, or empty if the string isnull
.
-
equalsIgnoreCase
Compares two strings to make sure that the strings are equal without regard to case, or that the strings are both set tonull
. If the first string is notnull
, it is compared to the second using the first string'sString.equalsIgnoreCase(String)
method. This is a convenience method to compare two strings using theString.equalsIgnoreCase(String)
method when it's not known if one of the strings isnull
.- Parameters:
string1
- The first string to compare.string2
- The second string to compare.- Returns:
true
if the strings are equal according to the first string'sString.equalsIgnoreCase(String)
method or if both strings arenull
.- See Also:
-
getASCIIZBytes
Creates an array of bytes of the specified length and stores the bytes of the string, in UTF-8, in the array appended with a zero byte. If the string is too long for the given length, it is truncated.- Parameters:
string
- The string to store in bytes.length
- The length of bytes to return.- Returns:
- The array of bytes containing the bytes of the given string.
- Throws:
UnsupportedEncodingException
- Thrown if the given encoding is not supported.
-
getASCIIZBytes
public static byte[] getASCIIZBytes(String string, int length, Charset charset) throws UnsupportedEncodingException Creates an array of bytes of the specified length and stores the bytes of the string, in the array appended with a zero byte. If the string is too long for the given length, it is truncated.- Parameters:
string
- The string to store in bytes.length
- The length of bytes to return.charset
- The charset to use in storing the string bytes.- Returns:
- The array of bytes containing the bytes of the given string.
- Throws:
UnsupportedEncodingException
- Thrown if the given encoding is not supported.
-
getInputStream
public static InputStream getInputStream(String string, String encoding) throws UnsupportedEncodingException Creates an input stream from which to read the given string.- Parameters:
string
- The string for which an input stream should be created.encoding
- The encoding to use to turn the string into bytes.- Returns:
- An input stream of the string bytes.
- Throws:
UnsupportedEncodingException
- Thrown if the given encoding is not supported.
-
indexOfIgnoreCase
Finds the index of a particular character, ignoring case.- Parameters:
string
- The string to search.c
- The character to search for.- Returns:
- The index of the character ignoring case, or -1 if the character could not be found.
-
indexOfIgnoreCase
Finds the index of a particular character, ignoring case, from a given index.- Parameters:
string
- The string to search.c
- The character to search for.fromIndex
- The index at which to begin searching- Returns:
- The index of the character ignoring case, or -1 if the character could not be found.
-
indexOfIgnoreCase
Finds the index of a particular substring, ignoring case.- Parameters:
string
- The string to search.substring
- The string to search for.- Returns:
- The index of the substring ignoring case, or -1 if the substring could not be found.
-
indexOfIgnoreCase
Finds the index of a particular substring, ignoring case, from a given index.- Parameters:
string
- The string to search.substring
- The string to search for.fromIndex
- The index at which to begin searching- Returns:
- The index of the substring ignoring case, or -1 if the substring could not be found.
-
insert
Inserts a string at a specified index.- Parameters:
inString
- the String into which the information will be inserted.index
- The information will be inserted before the character that appears at this index.insertString
- The string to insert.- Returns:
- A new string with the specified information inserted at the specified location.
-
insert
Inserts a character at a specified index.- Parameters:
inString
- the String into which the information will be inserted.index
- The information will be inserted before the character that appears at this index.insertChar
- The character to insert.- Returns:
- A new string with the specified information inserted at the specified location.
-
lastIndexOfIgnoreCase
Searches a string in reverse and returns the last index of a substring without case sensitivity, starting fromfromIndex
.- Parameters:
string
- The string to be searched.substring
- The substring for which to search.fromIndex
- The index from which to search.- Returns:
- The index of the last occurrence of the substring at or less than the given index, or -1 if none was found.
-
startsWithIgnoreCase
Determines whether the string starts with the given prefix, ignoring case.- Parameters:
string
- The string to search.prefix
- The starting string to check for.- Returns:
true
if the string starts with the given prefix, ignoring case.
-
tokenIndex
Returns the index of the given numbered (one-based) token.- Parameters:
inString
- The string to search.tokenNumber
- The number (one-based) of the token to find.delimiters
- The characters to use for delimiters.- Returns:
- The index of the specified token number, or -1 if that token was not found.
-
tokenEndIndex
Returns the index right after the given numbered (one-based) token.- Parameters:
inString
- The string to search.tokenNumber
- The number (one-based) of the token to find.delimiters
- The characters to use for delimiters.- Returns:
- The index of one character past the last character of the specified token number, or -1 if that token was not found.
-
stringToken
Returns the specified numbered (one-based) token in the specified string, separated by delimiters.- Parameters:
inString
- The string to search.tokenNumber
- The number (one-based) of the token to find.delimiters
- The characters to use for delimiters.- Returns:
- The specified numbered (one-based) token in the specified string, separated by delimiters, or "" if that token was not found.
-
wordIndex
Returns the index of the given numbered (one-based) word.- Parameters:
inString
- The string to search.wordNumber
- The number (one-based) of the token to find.- Returns:
- The index of the specified word, or -1 if that word was not found.
-
wordEndIndex
Returns the index right after the given numbered (one-based) word.- Parameters:
inString
- The string to search.wordNumber
- The number (one-based) of the word to find.- Returns:
- The index of one character past the last character of the specified word number, or -1 if that word was not found.
-
stringWord
Returns the specified numbered (one-based) word in the specified string.- Parameters:
inString
- The string to search.wordNumber
- The number (one-based) of the word to find.- Returns:
- The specified numbered (one-based) word in the specified string, or "" if that word was not found.
-
getWordBeginning
Returns the beginning of the word at index. If the character at index is whitespace, the beginning of the previous word will be returned.- Parameters:
inString
- The string with the word.index
- The index of the character in a word.- Returns:
- The index of the beginning character of the word.
-
getWordEnd
Returns the end of the word at index. If the character at index is whitespace, the end of the next word will be returned.- Parameters:
inString
- The string with the word.index
- The index of the character in a word.- Returns:
- The index of the ending character of the word.
-
makeHTMLHyperlinks
Parses the string and finds any hyperlinks, to which it then creates and inserts the appropriate HTML tags.- Parameters:
inString
- The string in which to find hyperlinks.- Returns:
- A string with HTML hyperlink tags embedded.
-
createString
Creates a string containing a range of characters.- Parameters:
firstChar
- The character to start with.lastChar
- The last character to include, which should have a value higher than or equal tofirstChar
.- Returns:
- A string containing a range of characters including the first and last characters provided.
-
createString
Creates a string with a given repetition of characters.- Parameters:
ch
- The character to be in the string.count
- The number of repetitions of the character.- Returns:
- A string with count repetitions of ch.
-
makeStringLength
Deprecated.to be removed in favor offorceLength(String, int, char)
.Ensures that the given string is the correct length by adding or deleting characters to or from the end.- Parameters:
inString
- The string to process.len
- The requested length.ch
- The character to be added to the string, if needed.- Returns:
- A string with the correct length.
-
forceLength
Ensures that the given string is the correct length by adding or deleting characters to or from the end.- Parameters:
inString
- The string to process.len
- The requested length.ch
- The character to be added to the string, if needed.- Returns:
- A string with the correct length.
- See Also:
-
makeStringLength
Deprecated.to be removed in favor offorceLength(String, int, char, int)
.Ensures that the given string is the correct length by adding or deleting characters to or from the requested position.- Parameters:
inString
- The string to process.len
- The requested length.ch
- The character to be added to the string, if needed.pos
- The position at which to insert or delete characters, or -1 if the end should be used.- Returns:
- A string with the correct length.
-
forceLength
Ensures that the given string is the correct length by adding or deleting characters to or from the requested position.- Parameters:
inString
- The string to process.len
- The requested length.ch
- The character to be added to the string, if needed.pos
- The position at which to insert or delete characters, or -1 if the end should be used.- Returns:
- A string with the correct length.
- See Also:
-
remove
Removes a character at the specified index.- Parameters:
inString
- the String from which the information will be removed.index
- The index of the information to remove.- Returns:
- A new string with the specified information removed from the specified location.
-
remove
Removes several characters at the specified index.- Parameters:
inString
- the String from which the information will be removed.index
- The index of the information to remove.len
- The number of characters to remove.- Returns:
- A new string with the specified information removed from the specified location.
-
removeAfterFirstChar
Removes all characters that come after and including the first occurrence of a character in the given delimiter string. If the character does not exist in the string, the original string will be returned.- Parameters:
string
- The string to check.delimiters
- The characters that indicate removal should occur.- Returns:
- The string with the first matching character and everything after it removed, or the original string if no characters were in the supplied set of delimiters.
-
removeBeforeLast
Removes all characters that come before the last occurrence of the given character. If the character does not exist in the string, the original string will be returned.- Parameters:
string
- The string to be used to create a new string after the given character.c
- The character used as a delimiter to be removed with all the content before it.- Returns:
- The string without the characters before the given character.
-
removeLengthIgnoreCase
Removes the given substring, matched without case sensitivity, and everything following the string.- Parameters:
string
- The string that may contain the substring.substring
- The string to match without case sensitivity.- Returns:
- The string with the substring and everything following it removed, or the original string if no changes were made.
-
trimBeginningFirst
Trims the left side of the string beginning at the first occurrence of removeChar. If removeChar does not exist in the string, no information is removed.- Parameters:
inString
- the String from which the information will be removed. The occurrences are considered from the left to the right.removeChar
- The character to remove from the string.- Returns:
- A new string with its beginning removed.
-
trimBeginningLast
Trims the left side of the string beginning at the last occurrence of removeChar. If removeChar does not exist in the string, no information is removed.- Parameters:
inString
- the String from which the information will be removed. The occurrences are considered from the left to the right.removeChar
- The character to remove from the string.- Returns:
- A new string with its beginning removed.
-
trimBeginning
Trims the left side of the string beginning at the specified occurrence of removeChar from the beginning. If removeChar does not exist in the string the required number of times, the string will be trimmed at the last occurrence. The occurrences are considered from the left to the right. IfInteger.MAX_VALUE
is provided, the last occurrence from the left to the right of remove char is used.- Parameters:
inString
- the String from which the information will be removed.removeChar
- The character to remove from the string.occurrence
- The number of occurrences of the remove character before information should be removed.- Returns:
- A new string with its end removed.
-
trimEndFirst
Trims the right side of the string beginning at the first occurrence of removeChar. If removeChar does not exist in the string, no information is removed.- Parameters:
inString
- the String from which the information will be removed. The occurrences are considered from the right to the left.removeChar
- The character to remove from the string.- Returns:
- A new string with its end removed.
-
trimEndLast
Trims the right side of the string beginning at the last occurrence of removeChar. If removeChar does not exist in the string, no information is removed.- Parameters:
inString
- the String from which the information will be removed. The occurrences are considered from the right to the left.removeChar
- The character to remove from the string.- Returns:
- A new string with its end removed.
-
trimEnd
Trims the right side of the string beginning at the specified occurrence of removeChar from the end. If removeChar does not exist in the string the required number of times, the string will be trimmed at the last occurrence. The occurrences are considered from the right to the left. IfInteger.MAX_VALUE
is provided, the last occurrence from the right to the left of remove char is used.- Parameters:
inString
- the String from which the information will be removed.removeChar
- The character to remove from the string.occurrence
- The number of occurrences of the remove character before information should be removed.- Returns:
- A new string with its end removed.
-
replace
Removes several characters at the specified index and replaces them with the given string.- Parameters:
inString
- the String from which the information will be removed.index
- The index of the information to remove.len
- The number of characters to remove.replaceString
- The string of characters to put in the place of the removed characters.- Returns:
- A new string with the specified information removed from the specified location.
-
replace
Replaces every occurrence of a specified character with a string.- Parameters:
inString
- the String from in the information will be replaced.replaceChar
- The character to replace.withString
- The string that will replace replaceChar.- Returns:
- A new string with the specified information replaced.
-
replace
Replaces every occurrence of a specified string with another string.- Parameters:
inString
- the String from in the information will be replaced.replaceString
- The string to replace.withString
- The string that will replace replaceString.- Returns:
- A new string with the specified information replaced.
-
replace
Replaces any of several matching characters with a particular character.- Parameters:
inString
- The string in which characters should be replaced.matchCharacters
- The string containing characters to be matched; every character that matches one of these characters will be replaced with the replacement character.replacementChar
- The character to replace any matched character.- Returns:
- A string with the appropriate characters replaced by the replacement character.
-
replace
Replaces any of several matching characters with a particular string.- Parameters:
inString
- The string in which characters should be replaced.matchCharacters
- The string containing characters to be matched; every character that matches one of these characters will be replaced with the replacement string.replacementString
- The string to replace any matched character.- Returns:
- A string with the appropriate characters replaced by the replacement string.
-
replace
Replaces each matching character with the corresponding replacement string.- Parameters:
string
- The string in which the replacements will be made.matchChars
- An array of characters to be replaced.replacementStrings
- An array of strings to replace the characters appearing at the same indexes as those in matchChars.- Returns:
- The string with replacements made, which may be the original string if no replacements were made.
-
truncate
Truncates the string, if needed, to ensure that the string is no longer than the provided length.- Parameters:
string
- The string to truncate.maxLength
- The maximum length of the string; if the string is longer than the given length, it will be truncated.- Returns:
- The string, if the string was shorter than or equal to the maximum length; otherwise, the first
maxLength
characters of the string.
-
truncateChar
Removes everything after and including the first occurrence of one of the given characters.- Parameters:
string
- The string that may contain one or more of the characters.delimiters
- The characters that will cause- Returns:
- The string with the first occurring character and everything after it removed, or the original string if no changes were made.
-
collapse
Collapses every run of any number of collapseChars to a single replacement string.- Parameters:
inString
- the String in which the information will be collapsed.collapseChars
- The characters to be removed from the string.replaceString
- The string which will replace the collapseChars.- Returns:
- A new string with the specified information collapsed.
-
trim
Trims the specified delimiters from the beginning and end of the string.- Parameters:
inString
- The string to be processed.delimiters
- The string containing delimiter characters.- Returns:
- The string without the delimiters from the beginning and end of the string.
-
trimWhitespaceNoBreak
Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning and end of the string.- Parameters:
inString
- The string to be processed.- Returns:
- The string without any whitespace on the beginning or the end of the string.
-
trimWhitespaceNoBreakBeginning
Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning of the string.- Parameters:
inString
- The string to be processed.- Returns:
- The string without any spaces on the beginning of the string.
-
trimWhitespaceNoBreakEnd
Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning of the string.- Parameters:
inString
- The string to be processed.- Returns:
- The string without any spaces on the end of the string.
-
trimBeginning
If the input string begins with the specifid string, trims that string.- Parameters:
inString
- The string to be processed.beginString
- The string to be trimmed, if it appears at the beginning of inString.- Returns:
- The string, trimmed if needed.
-
trimEnd
If the input string ends with the specified string, trims that string.- Parameters:
inString
- The string to be processed.endString
- The string to be trimmed, if it appears at the end of inString.- Returns:
- The string, trimmed if needed.
-
wrap
Performs word-wrapping on the input string by strategically inserting ends-of-line, ensuring that each line is no longer than the specified length.- Parameters:
inString
- The string to be wrapped.wrapLength
- The maximum length of any line.- Returns:
- The string wrapped with the given characters inserted.
-
wrap
Performs word-wrapping on the input string by strategically inserting ends-of-line, ensuring that each line is no longer than the specified length.- Parameters:
inString
- The string to be wrapped.wrapLength
- The maximum length of any line.padChar
- The character to use to pad each line, orUNDEFINED_CHAR
if lines should not be padded.eolChar
- The string to insert at the end of each line, orUNDEFINED_CHAR
if ends of lines should not be marked.- Returns:
- The string wrapped with the given characters inserted.
-
write
Writes an object to a string using the given I/O support, converting bytes to a string using the UTF-8 charset.- Type Parameters:
T
- The type of the object- Parameters:
baseURI
- The base URI of the data, ornull
if no base URI is available.object
- The object to write to a string.io
- The I/O support for writing the object.- Returns:
- The given object converted to a string.
- Throws:
IOException
- if there is an error writing the data.
-
write
Writes an object to a string using the given I/O support.- Type Parameters:
T
- The type of the object- Parameters:
baseURI
- The base URI of the data, ornull
if no base URI is available.object
- The object to write to a string.io
- The I/O support for writing the object.charset
- The encoding with which to interpret the written bytes.- Returns:
- The given object converted to a string.
- Throws:
IOException
- if there is an error writing the data.
-
forceLength(String, int, char)
.