Class Strings


  • public class Strings
    extends java.lang.Object
    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:
    StringBuilders
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String[] NO_STRINGS
      A shared empty array of strings.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String asNonEmptyString​(java.lang.String string)
      Returns the string or null if the string is the empty string ("").
      static java.lang.String collapse​(java.lang.String inString, Characters collapseChars, java.lang.String replaceString)
      Collapses every run of any number of collapseChars to a single replacement string.
      static java.lang.String[] createArray​(java.lang.String string)
      Creates an array of strings from the given string.
      static java.lang.String createString​(char firstChar, char lastChar)
      Creates a string containing a range of characters.
      static java.lang.String createString​(char ch, int count)
      Creates a string with a given repetition of characters.
      static boolean equalsIgnoreCase​(java.lang.String string1, java.lang.String string2)
      Compares two strings to make sure that the strings are equal without regard to case, or that the strings are both set to null.
      static byte[] getASCIIZBytes​(java.lang.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​(java.lang.String string, int length, java.nio.charset.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 java.io.InputStream getInputStream​(java.lang.String string, java.lang.String encoding)
      Creates an input stream from which to read the given string.
      static int getWordBeginning​(java.lang.String inString, int index)
      Returns the beginning of the word at index.
      static int getWordEnd​(java.lang.String inString, int index)
      Returns the end of the word at index.
      static int indexOfIgnoreCase​(java.lang.String string, char c)
      Finds the index of a particular character, ignoring case.
      static int indexOfIgnoreCase​(java.lang.String string, char c, int fromIndex)
      Finds the index of a particular character, ignoring case, from a given index.
      static int indexOfIgnoreCase​(java.lang.String string, java.lang.String substring)
      Finds the index of a particular substring, ignoring case.
      static int indexOfIgnoreCase​(java.lang.String string, java.lang.String substring, int fromIndex)
      Finds the index of a particular substring, ignoring case, from a given index.
      static java.lang.String insert​(java.lang.String inString, int index, char insertChar)
      Inserts a character at a specified index.
      static java.lang.String insert​(java.lang.String inString, int index, java.lang.String insertString)
      Inserts a string at a specified index.
      static int lastIndexOfIgnoreCase​(java.lang.String string, java.lang.String substring, int fromIndex)
      Searches a string in reverse and returns the last index of a substring without case sensitivity, starting from fromIndex.
      static java.lang.String makeHTMLHyperlinks​(java.lang.String inString)
      Parses the string and finds any hyperlinks, to which it then creates and inserts the appropriate HTML tags.
      static java.lang.String makeStringLength​(java.lang.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 java.lang.String makeStringLength​(java.lang.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 java.lang.String remove​(java.lang.String inString, int index)
      Removes a character at the specified index.
      static java.lang.String remove​(java.lang.String inString, int index, int len)
      Removes several characters at the specified index.
      static java.lang.String removeAfterFirstChar​(java.lang.String string, Characters delimiters)
      Removes all characters that come after and including the first occurrence of a character in the given delimiter string.
      static java.lang.String removeBeforeLast​(java.lang.String string, char c)
      Removes all characters that come before the last occurrence of the given character.
      static java.lang.String removeLengthIgnoreCase​(java.lang.String string, java.lang.String substring)
      Removes the given substring, matched without case sensitivity, and everything following the string.
      static java.lang.String replace​(java.lang.String string, char[] matchChars, java.lang.String[] replacementStrings)
      Replaces each matching character with the corresponding replacement string.
      static java.lang.String replace​(java.lang.String inString, char replaceChar, java.lang.String withString)
      Replaces every occurrence of a specified character with a string.
      static java.lang.String replace​(java.lang.String inString, int index, int len, java.lang.String replaceString)
      Removes several characters at the specified index and replaces them with the given string.
      static java.lang.String replace​(java.lang.String inString, Characters matchCharacters, char replacementChar)
      Replaces any of several matching characters with a particular character.
      static java.lang.String replace​(java.lang.String inString, Characters matchCharacters, java.lang.String replacementString)
      Replaces any of several matching characters with a particular string.
      static java.lang.String replace​(java.lang.String inString, java.lang.String replaceString, java.lang.String withString)
      Replaces every occurrence of a specified string with another string.
      static boolean startsWithIgnoreCase​(java.lang.String string, java.lang.String prefix)
      Determines whether the string starts with the given prefix, ignoring case.
      static java.lang.String stringOf​(char... chars)
      Convenience method to create a string from characters using varargs.
      static java.lang.String stringToken​(java.lang.String inString, int tokenNumber, Characters delimiters)
      Returns the specified numbered (one-based) token in the specified string, separated by delimiters.
      static java.lang.String stringWord​(java.lang.String inString, int wordNumber)
      Returns the specified numbered (one-based) word in the specified string.
      static int tokenEndIndex​(java.lang.String inString, int tokenNumber, Characters delimiters)
      Returns the index right after the given numbered (one-based) token.
      static int tokenIndex​(java.lang.String inString, int tokenNumber, Characters delimiters)
      Returns the index of the given numbered (one-based) token.
      static java.lang.String trim​(java.lang.String inString, Characters delimiters)
      Trims the specified delimiters from the beginning and end of the string.
      static java.lang.String trimBeginning​(java.lang.String inString, char removeChar, int occurrence)
      Trims the left side of the string beginning at the specified occurrence of removeChar from the beginning.
      static java.lang.String trimBeginning​(java.lang.String inString, java.lang.String beginString)
      If the input string begins with the specifid string, trims that string.
      static java.lang.String trimBeginningFirst​(java.lang.String inString, char removeChar)
      Trims the left side of the string beginning at the first occurrence of removeChar.
      static java.lang.String trimBeginningLast​(java.lang.String inString, char removeChar)
      Trims the left side of the string beginning at the last occurrence of removeChar.
      static java.lang.String trimEnd​(java.lang.String inString, char removeChar, int occurrence)
      Trims the right side of the string beginning at the specified occurrence of removeChar from the end.
      static java.lang.String trimEnd​(java.lang.String inString, java.lang.String endString)
      If the input string ends with the specified string, trims that string.
      static java.lang.String trimEndFirst​(java.lang.String inString, char removeChar)
      Trims the right side of the string beginning at the first occurrence of removeChar.
      static java.lang.String trimEndLast​(java.lang.String inString, char removeChar)
      Trims the right side of the string beginning at the last occurrence of removeChar.
      static java.lang.String trimWhitespaceNoBreak​(java.lang.String inString)
      Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning and end of the string.
      static java.lang.String trimWhitespaceNoBreakBeginning​(java.lang.String inString)
      Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning of the string.
      static java.lang.String trimWhitespaceNoBreakEnd​(java.lang.String inString)
      Trims whitespace, including the Unicode no-break space character 0x00A0, from the beginning of the string.
      static java.lang.String truncate​(java.lang.String string, int maxLength)
      Truncates the string, if needed, to ensure that the string is no longer than the provided length.
      static java.lang.String truncateChar​(java.lang.String string, Characters delimiters)
      Removes everything after and including the first occurrence of one of the given characters.
      static int wordEndIndex​(java.lang.String inString, int wordNumber)
      Returns the index right after the given numbered (one-based) word.
      static int wordIndex​(java.lang.String inString, int wordNumber)
      Returns the index of the given numbered (one-based) word.
      static java.lang.String wrap​(java.lang.String inString, int wrapLength)
      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 java.lang.String wrap​(java.lang.String inString, int wrapLength, char padChar, char eolChar)
      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> java.lang.String write​(java.net.URI baseURI, T object, IO<T> io)
      Writes an object to a string using the given I/O support, converting bytes to a string using the UTF-8 charset.
      static <T> java.lang.String write​(java.net.URI baseURI, T object, IO<T> io, java.nio.charset.Charset charset)
      Writes an object to a string using the given I/O support.
      • Methods inherited from class java.lang.Object

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

      • NO_STRINGS

        public static final java.lang.String[] NO_STRINGS
        A shared empty array of strings.
    • Method Detail

      • asNonEmptyString

        public static java.lang.String asNonEmptyString​(java.lang.String string)
        Returns the string or null 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, otherwise null.
      • stringOf

        public static final java.lang.String stringOf​(char... chars)
        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:
        java.lang.NullPointerException - if the given chars is null.
      • createArray

        public static java.lang.String[] createArray​(java.lang.String string)
        Creates an array of strings from the given string.
        Parameters:
        string - The string to include in the array, or null if the array should be empty.
        Returns:
        A non-null array containing the string, or empty if the string is null.
      • equalsIgnoreCase

        public static final boolean equalsIgnoreCase​(java.lang.String string1,
                                                     java.lang.String string2)
        Compares two strings to make sure that the strings are equal without regard to case, or that the strings are both set to null. If the first string is not null, it is compared to the second using the first string's String.equalsIgnoreCase(String) method. This is a convenience method to compare two strings using the String.equalsIgnoreCase(String) method when it's not known if one of the strings is null.
        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's String.equalsIgnoreCase(String) method or if both strings are null.
        See Also:
        String.equalsIgnoreCase(String)
      • getASCIIZBytes

        public static byte[] getASCIIZBytes​(java.lang.String string,
                                            int length)
                                     throws java.io.UnsupportedEncodingException
        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:
        java.io.UnsupportedEncodingException - Thrown if the given encoding is not supported.
      • getASCIIZBytes

        public static byte[] getASCIIZBytes​(java.lang.String string,
                                            int length,
                                            java.nio.charset.Charset charset)
                                     throws java.io.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:
        java.io.UnsupportedEncodingException - Thrown if the given encoding is not supported.
      • getInputStream

        public static java.io.InputStream getInputStream​(java.lang.String string,
                                                         java.lang.String encoding)
                                                  throws java.io.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:
        java.io.UnsupportedEncodingException - Thrown if the given encoding is not supported.
      • indexOfIgnoreCase

        public static int indexOfIgnoreCase​(java.lang.String string,
                                            char c)
        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

        public static int indexOfIgnoreCase​(java.lang.String string,
                                            char c,
                                            int fromIndex)
        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

        public static int indexOfIgnoreCase​(java.lang.String string,
                                            java.lang.String substring)
        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

        public static int indexOfIgnoreCase​(java.lang.String string,
                                            java.lang.String substring,
                                            int fromIndex)
        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

        public static java.lang.String insert​(java.lang.String inString,
                                              int index,
                                              java.lang.String insertString)
        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

        public static java.lang.String insert​(java.lang.String inString,
                                              int index,
                                              char insertChar)
        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

        public static int lastIndexOfIgnoreCase​(java.lang.String string,
                                                java.lang.String substring,
                                                int fromIndex)
        Searches a string in reverse and returns the last index of a substring without case sensitivity, starting from fromIndex.
        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

        public static boolean startsWithIgnoreCase​(java.lang.String string,
                                                   java.lang.String prefix)
        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

        public static int tokenIndex​(java.lang.String inString,
                                     int tokenNumber,
                                     Characters delimiters)
        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

        public static int tokenEndIndex​(java.lang.String inString,
                                        int tokenNumber,
                                        Characters delimiters)
        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

        public static java.lang.String stringToken​(java.lang.String inString,
                                                   int tokenNumber,
                                                   Characters delimiters)
        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

        public static int wordIndex​(java.lang.String inString,
                                    int wordNumber)
        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

        public static int wordEndIndex​(java.lang.String inString,
                                       int wordNumber)
        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

        public static java.lang.String stringWord​(java.lang.String inString,
                                                  int wordNumber)
        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

        public static int getWordBeginning​(java.lang.String inString,
                                           int index)
        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

        public static int getWordEnd​(java.lang.String inString,
                                     int index)
        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

        public static java.lang.String makeHTMLHyperlinks​(java.lang.String inString)
        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

        public static java.lang.String createString​(char firstChar,
                                                    char lastChar)
        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 to firstChar.
        Returns:
        A string containing a range of characters including the first and last characters provided.
      • createString

        public static java.lang.String createString​(char ch,
                                                    int count)
        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

        public static java.lang.String makeStringLength​(java.lang.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.
        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.
      • makeStringLength

        public static java.lang.String makeStringLength​(java.lang.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.
        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.
      • remove

        public static java.lang.String remove​(java.lang.String inString,
                                              int index)
        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

        public static java.lang.String remove​(java.lang.String inString,
                                              int index,
                                              int len)
        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

        public static java.lang.String removeAfterFirstChar​(java.lang.String string,
                                                            Characters delimiters)
        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

        public static java.lang.String removeBeforeLast​(java.lang.String string,
                                                        char c)
        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

        public static java.lang.String removeLengthIgnoreCase​(java.lang.String string,
                                                              java.lang.String substring)
        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

        public static java.lang.String trimBeginningFirst​(java.lang.String inString,
                                                          char removeChar)
        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

        public static java.lang.String trimBeginningLast​(java.lang.String inString,
                                                         char removeChar)
        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

        public static java.lang.String trimBeginning​(java.lang.String inString,
                                                     char removeChar,
                                                     int occurrence)
        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. If Integer.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

        public static java.lang.String trimEndFirst​(java.lang.String inString,
                                                    char removeChar)
        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

        public static java.lang.String trimEndLast​(java.lang.String inString,
                                                   char removeChar)
        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

        public static java.lang.String trimEnd​(java.lang.String inString,
                                               char removeChar,
                                               int occurrence)
        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. If Integer.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

        public static java.lang.String replace​(java.lang.String inString,
                                               int index,
                                               int len,
                                               java.lang.String replaceString)
        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

        public static java.lang.String replace​(java.lang.String inString,
                                               char replaceChar,
                                               java.lang.String withString)
        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

        public static java.lang.String replace​(java.lang.String inString,
                                               java.lang.String replaceString,
                                               java.lang.String withString)
        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

        public static java.lang.String replace​(java.lang.String inString,
                                               Characters matchCharacters,
                                               char replacementChar)
        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

        public static java.lang.String replace​(java.lang.String inString,
                                               Characters matchCharacters,
                                               java.lang.String replacementString)
        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

        public static java.lang.String replace​(java.lang.String string,
                                               char[] matchChars,
                                               java.lang.String[] replacementStrings)
        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

        public static java.lang.String truncate​(java.lang.String string,
                                                int maxLength)
        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

        public static java.lang.String truncateChar​(java.lang.String string,
                                                    Characters delimiters)
        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

        public static java.lang.String collapse​(java.lang.String inString,
                                                Characters collapseChars,
                                                java.lang.String replaceString)
        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

        public static java.lang.String trim​(java.lang.String inString,
                                            Characters delimiters)
        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

        public static java.lang.String trimWhitespaceNoBreak​(java.lang.String inString)
        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

        public static java.lang.String trimWhitespaceNoBreakBeginning​(java.lang.String inString)
        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

        public static java.lang.String trimWhitespaceNoBreakEnd​(java.lang.String inString)
        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

        public static java.lang.String trimBeginning​(java.lang.String inString,
                                                     java.lang.String beginString)
        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

        public static java.lang.String trimEnd​(java.lang.String inString,
                                               java.lang.String endString)
        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

        public static java.lang.String wrap​(java.lang.String inString,
                                            int wrapLength)
        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

        public static java.lang.String wrap​(java.lang.String inString,
                                            int wrapLength,
                                            char padChar,
                                            char eolChar)
        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, or UNDEFINED_CHAR if lines should not be padded.
        eolChar - The string to insert at the end of each line, or UNDEFINED_CHAR if ends of lines should not be marked.
        Returns:
        The string wrapped with the given characters inserted.
      • write

        public static <T> java.lang.String write​(java.net.URI baseURI,
                                                 T object,
                                                 IO<T> io)
                                          throws java.io.IOException
        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, or null 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:
        java.io.IOException - if there is an error writing the data.
      • write

        public static <T> java.lang.String write​(java.net.URI baseURI,
                                                 T object,
                                                 IO<T> io,
                                                 java.nio.charset.Charset charset)
                                          throws java.io.IOException
        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, or null 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:
        java.io.IOException - if there is an error writing the data.