Class StringUtil


  • public class StringUtil
    extends java.lang.Object
    Useful string utilities
    Version:
    1.7
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String newLine  
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String capitalize​(java.lang.String s)
      Capitalizes the given string.
      static boolean contains​(java.lang.String string, java.lang.String substring)
      Returns true if substring is indeed a substring of string.
      static boolean containsIgnoreCase​(java.lang.String string, java.lang.String substring)
      Returns true if substring is indeed a substring of string.
      static java.lang.String currentMethodName()  
      private static java.lang.String decodeStartingVowels​(java.lang.String s)
      Decodes strings returned by #encodeStartingVowels
      private static java.lang.String encodeStartingVowels​(java.lang.String s)
      Replaces vowels that start words with a special code
      static java.lang.String escapeHTML​(java.lang.String s)  
      static java.lang.String format​(double d, int decimals)
      Returns d as a string truncated to the specified number of decimal places
      static java.util.List fromCommaDelimitedString​(java.lang.String s)
      Converts the comma-delimited string into a List of trimmed strings.
      static java.lang.String getStackTrace​(java.lang.Throwable t)
      Returns an throwable's stack trace
      static java.lang.String getStackTrace​(java.lang.Throwable t, int depth)  
      static java.lang.String getTimeString​(long milliseconds)
      Converts the milliseconds value into a String of the form "9d 22h 15m 8s".
      static java.lang.String indent​(java.lang.String original, int spaces)  
      static int indexOfIgnoreParentheses​(char c, java.lang.String s, int start)
      Returns the position of the first occurrence of the given character found in s starting at start.
      static boolean isInteger​(java.lang.String s)
      Returns true if s can be converted to an int.
      static java.lang.String leftPad​(java.lang.String original, int length, char padChar)
      Pads the String with the given character until it has the given length.
      static java.lang.String removeConsecutiveDuplicates​(java.lang.String s)
      Replaces consecutive instances of characters with single instances.
      static java.lang.String removeFromEnd​(java.lang.String s, java.lang.String strToRemove)  
      static java.lang.String removeVowels​(java.lang.String s)
      Removes vowels from the string.
      static java.lang.String removeVowelsSkipStarts​(java.lang.String s)
      Removes vowels from the string except those that start words.
      static void replace​(java.lang.StringBuffer orig, java.lang.String o, java.lang.String n, boolean all)
      Replaces all instances of the String o with the String n in the StringBuffer orig if all is true, or only the first instance if all is false.
      static java.lang.String replace​(java.lang.String str, char oldChar, java.lang.String newStr)
      Returns a string with all occurrences of oldChar replaced by newStr
      static java.lang.String replace​(java.lang.String original, java.lang.String oldSubstring, java.lang.String newSubstring, boolean all)
      Returns original with occurrences of oldSubstring replaced by newSubstring.
      static java.lang.String replaceAll​(java.lang.String original, java.lang.String oldSubstring, java.lang.String newSubstring)
      Returns original with all occurrences of oldSubstring replaced by newSubstring
      static java.lang.String replaceFirst​(java.lang.String original, java.lang.String oldSubstring, java.lang.String newSubstring)
      Returns original with the first occurrenc of oldSubstring replaced by newSubstring
      static java.lang.String rightPad​(java.lang.String original, int length, char padChar)
      Pads the String with the given character until it has the given length.
      static java.lang.String stringOfChar​(char ch, int count)
      Returns a String of the given length consisting entirely of the given character
      static java.lang.String stripHTMLTags​(java.lang.String original)
      Removes the HTML tags from the given String, inserting line breaks at appropriate places.
      static java.lang.String toCommaDelimitedString​(java.util.Collection c)
      Returns the elements of c separated by commas.
      static java.lang.String toCommaDelimitedStringInQuotes​(java.util.Collection c)
      Returns the elements of c separated by commas and enclosed in single-quotes
      static java.util.Vector toLowerCase​(java.util.List v)
      Returns the elements of v in lowercase
      static java.lang.String toStringNeverNull​(java.lang.Object o)
      If s is null, returns "null"; otherwise, returns s.
      static java.util.Vector toUpperCase​(java.util.Vector v)
      Returns the elements of v in uppercase
      static java.lang.String wrap​(java.lang.String s, int n)
      Line-wraps a string s by inserting CR-LF instead of the first space after the nth columns.
      • Methods inherited from class java.lang.Object

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

      • newLine

        public static final java.lang.String newLine
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • removeFromEnd

        public static java.lang.String removeFromEnd​(java.lang.String s,
                                                     java.lang.String strToRemove)
      • capitalize

        public static java.lang.String capitalize​(java.lang.String s)
        Capitalizes the given string.
        Parameters:
        s - the string to capitalize
        Returns:
        the capitalized string
      • isInteger

        public static boolean isInteger​(java.lang.String s)
        Returns true if s can be converted to an int.
      • getStackTrace

        public static java.lang.String getStackTrace​(java.lang.Throwable t)
        Returns an throwable's stack trace
      • getStackTrace

        public static java.lang.String getStackTrace​(java.lang.Throwable t,
                                                     int depth)
      • getTimeString

        public static java.lang.String getTimeString​(long milliseconds)
        Converts the milliseconds value into a String of the form "9d 22h 15m 8s".
      • containsIgnoreCase

        public static boolean containsIgnoreCase​(java.lang.String string,
                                                 java.lang.String substring)
        Returns true if substring is indeed a substring of string. Case-insensitive.
      • contains

        public static boolean contains​(java.lang.String string,
                                       java.lang.String substring)
        Returns true if substring is indeed a substring of string.
      • replace

        public static java.lang.String replace​(java.lang.String str,
                                               char oldChar,
                                               java.lang.String newStr)
        Returns a string with all occurrences of oldChar replaced by newStr
      • stringOfChar

        public static java.lang.String stringOfChar​(char ch,
                                                    int count)
        Returns a String of the given length consisting entirely of the given character
      • indent

        public static java.lang.String indent​(java.lang.String original,
                                              int spaces)
      • toUpperCase

        public static java.util.Vector toUpperCase​(java.util.Vector v)
        Returns the elements of v in uppercase
      • toLowerCase

        public static java.util.Vector toLowerCase​(java.util.List v)
        Returns the elements of v in lowercase
      • toCommaDelimitedStringInQuotes

        public static java.lang.String toCommaDelimitedStringInQuotes​(java.util.Collection c)
        Returns the elements of c separated by commas and enclosed in single-quotes
      • toCommaDelimitedString

        public static java.lang.String toCommaDelimitedString​(java.util.Collection c)
        Returns the elements of c separated by commas. c must not be empty.
      • fromCommaDelimitedString

        public static java.util.List fromCommaDelimitedString​(java.lang.String s)
        Converts the comma-delimited string into a List of trimmed strings.
      • toStringNeverNull

        public static java.lang.String toStringNeverNull​(java.lang.Object o)
        If s is null, returns "null"; otherwise, returns s.
      • replace

        public static void replace​(java.lang.StringBuffer orig,
                                   java.lang.String o,
                                   java.lang.String n,
                                   boolean all)
        Replaces all instances of the String o with the String n in the StringBuffer orig if all is true, or only the first instance if all is false. Posted by Steve Chapel on UseNet
      • replaceAll

        public static java.lang.String replaceAll​(java.lang.String original,
                                                  java.lang.String oldSubstring,
                                                  java.lang.String newSubstring)
        Returns original with all occurrences of oldSubstring replaced by newSubstring
      • replaceFirst

        public static java.lang.String replaceFirst​(java.lang.String original,
                                                    java.lang.String oldSubstring,
                                                    java.lang.String newSubstring)
        Returns original with the first occurrenc of oldSubstring replaced by newSubstring
      • leftPad

        public static java.lang.String leftPad​(java.lang.String original,
                                               int length,
                                               char padChar)
        Pads the String with the given character until it has the given length. If original is longer than the given length, returns original.
      • rightPad

        public static java.lang.String rightPad​(java.lang.String original,
                                                int length,
                                                char padChar)
        Pads the String with the given character until it has the given length. If original is longer than the given length, returns original.
      • stripHTMLTags

        public static java.lang.String stripHTMLTags​(java.lang.String original)
        Removes the HTML tags from the given String, inserting line breaks at appropriate places. Needs a little work.
      • format

        public static java.lang.String format​(double d,
                                              int decimals)
        Returns d as a string truncated to the specified number of decimal places
      • wrap

        public static java.lang.String wrap​(java.lang.String s,
                                            int n)
        Line-wraps a string s by inserting CR-LF instead of the first space after the nth columns.
      • removeVowels

        public static java.lang.String removeVowels​(java.lang.String s)
        Removes vowels from the string. Case-insensitive.
      • removeVowelsSkipStarts

        public static java.lang.String removeVowelsSkipStarts​(java.lang.String s)
        Removes vowels from the string except those that start words. Case-insensitive.
      • removeConsecutiveDuplicates

        public static java.lang.String removeConsecutiveDuplicates​(java.lang.String s)
        Replaces consecutive instances of characters with single instances. Case-insensitive.
      • indexOfIgnoreParentheses

        public static int indexOfIgnoreParentheses​(char c,
                                                   java.lang.String s,
                                                   int start)
        Returns the position of the first occurrence of the given character found in s starting at start. Ignores text within pairs of parentheses. Returns -1 if no occurrence is found.
      • replace

        public static java.lang.String replace​(java.lang.String original,
                                               java.lang.String oldSubstring,
                                               java.lang.String newSubstring,
                                               boolean all)
        Returns original with occurrences of oldSubstring replaced by newSubstring. Set all to true to replace all occurrences, or false to replace the first occurrence only.
      • encodeStartingVowels

        private static java.lang.String encodeStartingVowels​(java.lang.String s)
        Replaces vowels that start words with a special code
      • decodeStartingVowels

        private static java.lang.String decodeStartingVowels​(java.lang.String s)
        Decodes strings returned by #encodeStartingVowels
      • escapeHTML

        public static java.lang.String escapeHTML​(java.lang.String s)
      • currentMethodName

        public static java.lang.String currentMethodName()