commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang Strings.java
Date Tue, 02 Jul 2002 22:00:30 GMT
scolebourne    2002/07/02 15:00:29

  Modified:    lang/src/java/org/apache/commons/lang Strings.java
  Log:
  Add equalsIgnoreCase()
  Remove parseObjectKey()
  Remove removeUnderScores()
  Rename isValid() to isNotEmpty()
  Javadoc
  Reorder methods into logical groups
  
  Revision  Changes    Path
  1.11      +360 -327  jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java
  
  Index: Strings.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Strings.java	2 Jul 2002 00:18:10 -0000	1.10
  +++ Strings.java	2 Jul 2002 22:00:29 -0000	1.11
  @@ -85,6 +85,7 @@
    * @author <a href="mailto:bayard@generationjava.com">Bayard</a>
    * @author <a href="mailto:ed@apache.org">Ed Korthof</a>
    * @author <a href="mailto:rand_mcneely@yahoo.com>Rand McNeely</a>
  + * @author <a href="mailto:scolebourne@joda.org>Stephen Colebourne</a>
    * @version $Id$
    */
   public class Strings
  @@ -92,143 +93,227 @@
       /**
        * The size of the buffer to use when working with I/O (4 kB).
        */
  -    //public static int CHAR_BUFFER_SIZE = 4 * FileUtils.ONE_KB;
       public static int CHAR_BUFFER_SIZE = 4 * 1024;
  +    
  +    // Empty
  +    //--------------------------------------------------------------------------
   
       /**
  -     * Trims text safely, dealing with <code>null</code> references by
  -     * converting them to <code>""</code> (the empty string).
  +     * Removes white space from both ends of this string, handling null
  +     * by returning an empty string.
        *
  -     * @param s The text to trim.
  -     * @return The trimmed text (never <code>null</code>).
  +     * @see java.lang.String#trim()
  +     * @param str  the string to check
  +     * @return the trimmed text (never <code>null</code>)
        */
  -    public static final String clean(String s)
  +    public static String clean(String str)
       {
  -        return (s == null ? "" : s.trim());
  +        return (str == null ? "" : str.trim());
       }
   
       /**
  -     * Trims text safely, dealing with <code>null</code> references by
  -     * returning <code>null</code> (rather than throwing a
  -     * NullPointerException).
  +     * Removes white space from both ends of this string, handling null
  +     * by returning null.
        *
  -     * @param s The text to trim.
  -     * @return The trimmed text (or <code>null</code>).
  +     * @see java.lang.String#trim()
  +     * @param str  the string to check
  +     * @return the trimmed text (or <code>null</code>)
        */
  -    public static final String trim(String s)
  +    public static String trim(String str)
       {
  -        return (s == null ? null : s.trim());
  +        return (str == null ? null : str.trim());
       }
   
       /**
  -     * Validates that the supplied string is neither <code>null</code>
  -     * nor the empty string.
  +     * Checks if a String is non null and is not empty (length > 0).
        *
  -     * @param text The text to check.
  -     * @return Whether valid.
  +     * @param str  the string to check
  +     * @return true if the String is non-null, and not length zero
        */
  -    public static final boolean isValid(String text)
  +    public static boolean isNotEmpty(String str)
       {
  -        return (text != null && text.length() > 0);
  +        return (str != null && str.length() > 0);
       }
   
       /**
  -     * Determine whether a (trimmed) string is empty
  +     * Checks if a (trimmed) String is null or empty.
        *
  -     * @param foo The text to check.
  -     * @return Whether empty.
  +     * @param str  the string to check
  +     * @return true if the String is null, or length zero once trimmed
        */
  -    public static final boolean isEmpty(String foo)
  +    public static boolean isEmpty(String str)
       {
  -        return (foo == null || foo.trim().length() == 0);
  +        return (str == null || str.trim().length() == 0);
       }
   
  +    // Equals and IndexOf
  +    //--------------------------------------------------------------------------
  +    
       /**
  -     * Returns the output of printStackTrace as a String.
  +     * Compares two Strings, returning true if they are equal.
  +     * Nulls are handled without exceptions. Two <code>null</code>
  +     * references are considered equal. Comparison is case sensitive.
        *
  -     * @param e The source to extract a stack trace from.
  -     * @return The extracted stack trace.
  +     * @param str1  the first string
  +     * @param str2  the second string
  +     * @return true if the Strings are equal, case sensitive, or both null
        */
  -    public static final String stackTrace(Throwable e)
  +    public static boolean equals(String str1, String str2)
       {
  -        String trace = null;
  -        try
  -        {
  -            // And show the Error Screen.
  -            ByteArrayOutputStream buf = new ByteArrayOutputStream();
  -            e.printStackTrace( new PrintWriter(buf, true) );
  -            trace = buf.toString();
  -        }
  -        catch (Exception ignored)
  -        {
  -        }
  -        return trace;
  +        return (str1 == null ? str2 == null : str1.equals(str2));
       }
   
       /**
  -     * Safely compares two <code>String</code> objects, returning
  -     * whether their values are the same.  Two <code>null</code>
  -     * references are considered equal.
  +     * Compares two Strings, returning true if they are equal ignoring case.
  +     * Nulls are handled without exceptions. Two <code>null</code>
  +     * references are considered equal. Comparison is case insensitive.
        *
  -     * @param s1 The first string.
  -     * @param s2 The second string.
  -     * @return Whether the values of both strings are the same.
  +     * @param str1  the first string
  +     * @param str2  the second string
  +     * @return true if the Strings are equal, case insensitive, or both null
        */
  -    public static boolean equals(String s1, String s2)
  +    public static boolean equalsIgnoreCase(String str1, String str2)
       {
  -        return (s1 == null ? s2 == null : s1.equals(s2));
  +        return (str1 == null ? str2 == null : str1.equalsIgnoreCase(str2));
       }
   
       /**
  -     * Takes a String of the form <code>substring[substring]substring</code>
  -     * and returns the three parsed substrings.
  -     *
  -     * @return A three element {@link java.lang.String} array populated by
  -     * any parsed object key components.
  +     * Find the earliest index of any of a set of potential substrings.
  +     * Null string will return -1.
  +     * 
  +     * @param str  the string to check
  +     * @param searchStrs  the strings to search for
  +     * @return the earliest index of any of the strings
        */
  -    public static String[] parseObjectKey(String s)
  -    {
  -        String[] objectKey = new String[3];
  -        StringTokenizer st = new StringTokenizer(s, "[]");
  -        int count = st.countTokens();
  -        if (count > 1)
  -        {
  -            objectKey[0] = st.nextToken();
  -            objectKey[1] = st.nextToken();
  -            if (count == 3)
  -            {
  -                objectKey[2] = st.nextToken();
  +    static public int indexOfAny(String str, String[] searchStrs) {
  +        if ((str == null) || (searchStrs == null)) {
  +            return -1;
  +        }
  +        int sz = searchStrs.length;
  +
  +        // String's can't have a MAX_VALUEth index.
  +        int ret = Integer.MAX_VALUE;
  +
  +        int tmp = 0;
  +        for (int i = 0; i < sz; i++) {
  +            tmp = str.indexOf(searchStrs[i]);
  +            if (tmp == -1) {
  +                continue;
  +            }
  +
  +            if (tmp < ret) {
  +                ret = tmp;
               }
           }
  -        return objectKey;
  +
  +        return (ret == Integer.MAX_VALUE) ? -1 : ret;
       }
   
       /**
  -     * Removes underscores from text.
  -     *
  -     * @param text The text to remove any underscores from.
  -     * @return The underscore-less text.
  -     * @deprecated Use replace(String, String, String) instead.
  -     */
  -    public static String removeUnderScores(String text)
  -    {
  -        return replace(text, "_", "", -1);
  +     * Find the latest index of any of a set of potential substrings.
  +     * Null string will return -1.
  +     * 
  +     * @param str  the string to check
  +     * @param searchStrs  the strings to search for
  +     * @return the last index of any of the strings
  +     */
  +    static public int lastIndexOfAny(String str, String[] searchStrs) {
  +        if ((str == null) || (searchStrs == null)) {
  +            return -1;
  +        }
  +        int sz = searchStrs.length;
  +        int ret = -1;
  +        int tmp = 0;
  +        for (int i = 0; i < sz; i++) {
  +            tmp = str.lastIndexOf(searchStrs[i]);
  +            if (tmp > ret) {
  +                ret = tmp;
  +            }
  +        }
  +        return ret;
       }
   
  +    // Substring
  +    //--------------------------------------------------------------------------
  +    
       /**
  -     * Makes the first letter capital and leaves the rest as is.
  -     *
  -     * @param text The text to modify.
  -     * @return The modified text.
  +     * Gets a substring of the specified string avoiding exceptions.
  +     * A negative start position can be used to start n characters from
  +     * the end of the string.
  +     * 
  +     * @param str  the string to get the substring from
  +     * @param start  the position to start from,  negative means 
  +     * count back from the end of the string by this many characters
  +     * @return substring from start position
        */
  -    public static String firstLetterCaps(String text)
  -    {
  -        return (text == null ? null :
  -                text.substring(0, 1).toUpperCase() + text.substring(1));
  +    public static String substring(String str, int start) {
  +        if (str == null) {
  +            return null;
  +        }
  +
  +        // handle negatives, which means last n characters
  +        if (start < 0) {
  +            start = str.length() + start; // remember start is negative
  +        }
  +
  +        if (start < 0) {
  +            start = 0;
  +        }
  +        if (start > str.length()) {
  +            return "";
  +        }
  +
  +        return str.substring(start);
  +    }
  +    
  +    /**
  +     * Gets a substring of the specified string avoiding exceptions.
  +     * A negative start position can be used to start/end n characters
  +     * from the end of the string.
  +     * 
  +     * @param str  the string to get the substring from
  +     * @param start  the position to start from, negative means 
  +     * count back from the end of the string by this many characters
  +     * @param end  the position to end at (exclusive),  negative means 
  +     * count back from the end of the string by this many characters
  +     * @return substring from start position to end positon
  +     */
  +    public static String substring(String str, int start, int end) {
  +        if (str == null) {
  +            return null;
  +        }
  +
  +        // handle negatives
  +        if (end < 0) {
  +            end = str.length() + end; // remember end is negative
  +        }
  +        if (start < 0) {
  +            start = str.length() + start; // remember start is negative
  +        }
  +
  +        // check length next
  +        if (end > str.length()) {
  +            // check this works.
  +            end = str.length();
  +        }
  +
  +        // if start is greater than end, return ""
  +        if (start > end) {
  +            return "";
  +        }
  +
  +        if (start < 0) {
  +            start = 0;
  +        }
  +        if (end < 0) {
  +            end = 0;
  +        }
  +
  +        return str.substring(start, end);
       }
   
       /**
  -     * Get the leftmost n characters of a string. If n characters are not 
  +     * Gets the leftmost n characters of a string. If n characters are not 
        * available, or the string is null, the string will be returned 
        * without an exception.
        *
  @@ -250,7 +335,7 @@
       }
   
       /**
  -     * Get the rightmost n characters of a string. If n characters are not 
  +     * Gets the rightmost n characters of a string. If n characters are not 
        * available, or the string is null, the string will be returned 
        * without an exception.
        *
  @@ -272,7 +357,7 @@
       }
   
       /**
  -     * Get n characters from the middle of a string. If n characters are 
  +     * Gets n characters from the middle of a string. If n characters are 
        * not available, the remainder of the string will be returned 
        * without an exception. If the string is null, null will be returned.
        *
  @@ -302,8 +387,15 @@
           }
       }
   
  +    // Splitting
  +    //--------------------------------------------------------------------------
  +    
       /**
  -     * @see #split(String text, String separator, int max)
  +     * Splits the provided text into a list, using whitespace as the separator.
  +     * The separator is not included in the returned String array.
  +     *
  +     * @param str  the string to parse
  +     * @return an array of parsed Strings 
        */
       public static String[] split(String text)
       {
  @@ -311,7 +403,14 @@
       }
   
       /**
  -     * @see #split(String text, String separator, int max)
  +     * Splits the provided text into a list, based on a given separator.
  +     * The separator is not included in the returned String array.
  +     * A null separator will cause parsing to be on whitespace.
  +     *
  +     * @param str  the string to parse
  +     * @param separator  The separator character. If <code>null</code>, splits
  +     *  on whitespace.
  +     * @return an array of parsed Strings 
        */
       public static String[] split(String text, String separator)
       {
  @@ -319,60 +418,51 @@
       }
   
       /**
  -     * Splits the provided text into a list, based on a given
  -     * separator.
  -     *
  -     * @param text Textual list to parse.
  -     * @param separator The separator character. If <code>null</code>, splits
  -     * on whitespace.
  -     * @param max The maximum number of elements to include in the
  -     * list.  A zero or negative value implies no limit.
  -     * @return The list of values.
  +     * Splits the provided text into a list, based on a given separator.
  +     * The separator is not included in the returned String array.
  +     * The maximum number of splits to perfom can be controlled.
  +     * A null separator will cause parsing to be on whitespace.
  +     *
  +     * @param str  the string to parse
  +     * @param separator  The separator character. If <code>null</code>, splits
  +     *  on whitespace.
  +     * @param max  The maximum number of elements to include in the
  +     *  list.  A zero or negative value implies no limit.
  +     * @return an array of parsed Strings 
        */
       public static String[] split(String text, String separator, int max)
       {
           StringTokenizer tok = null;
  -        if (separator == null)
  -        {
  +        if (separator == null) {
               // Null separator means we're using StringTokenizer's default
               // delimiter, which comprises all whitespace characters.
               tok = new StringTokenizer(text);
  -        }
  -        else
  -        {
  +        } else {
               tok = new StringTokenizer(text, separator);
           }
   
           int listSize = tok.countTokens();
  -        if (max > 0 && listSize > max)
  -        {
  +        if (max > 0 && listSize > max) {
               listSize = max;
           }
   
           String[] list = new String[listSize];
           int i = 0;
  -        while (tok.hasMoreTokens())
  -        {
  -            if (max > 0 && i == listSize - 1)
  -            {
  +        while (tok.hasMoreTokens()) {
  +            if (max > 0 && i == listSize - 1) {
                   // In the situation where we hit the max yet have
                   // tokens left over in our input, the last list
                   // element gets all remaining text.
  -                StringBuffer buf = new StringBuffer
  -                    ((int) 1.2 * text.length() * (listSize - i) / listSize);
  -                while (tok.hasMoreTokens())
  -                {
  +                StringBuffer buf = new StringBuffer((int) 1.2 * text.length() * (listSize
- i) / listSize);
  +                while (tok.hasMoreTokens()) {
                       buf.append(tok.nextToken());
  -                    if (tok.hasMoreTokens())
  -                    {
  +                    if (tok.hasMoreTokens()) {
                           buf.append(separator);
                       }
                   }
                   list[i] = buf.toString();
                   break;
  -            }
  -            else
  -            {
  +            } else {
                   list[i] = tok.nextToken();
               }
               i++;
  @@ -380,94 +470,98 @@
           return list;
       }
   
  +    // Joining
  +    //--------------------------------------------------------------------------
  +    
       /**
        * Joins the elements of the provided array into a single string
  -     * containing the provided list of elements.  No delimiter is
  -     * added before or after the list.
  -     *
  -     * @param list      The list of values to join together.
  -     * @param separator The separator character.
  -     * @return          The CSV text.
  -     */
  -    public static String join(Object[] list, String separator)
  -    {
  -        int listSize = list.length;
  -        int bufSize = (listSize == 0 ? 0 :(list[0].toString().length() +
  -                                           separator.length()) * listSize);
  +     * containing the provided list of elements. 
  +     * No delimiter is added before or after the list.
  +     * A null separator is the same as a blank String.
  +     *
  +     * @param array  the array of values to join together
  +     * @param separator  the separator character to use
  +     * @return the joined String
  +     */
  +    public static String join(Object[] array, String separator)
  +    {
  +        if (separator == null) {
  +            separator = "";
  +        }
  +        int arraySize = array.length;
  +        int bufSize = (arraySize == 0 ? 0 : (array[0].toString().length() +
  +                                 separator.length()) * arraySize);
           StringBuffer buf = new StringBuffer(bufSize);
  -            
  -        for (int i = 0; i < listSize; i++)
  -        {
  -            if (i > 0)
  -            {
  +
  +        for (int i = 0; i < arraySize; i++) {
  +            if (i > 0) {
                   buf.append(separator);
               }
  -            buf.append(list[i]);
  +            buf.append(array[i]);
           }
           return buf.toString();
       }
   
       /**
  -     * Merges the list of <code>Object</code> intances supplied by an
  -     * <code>Iterator</code> into a single piece text.  No delimiter
  -     * is added before or after the list.
  -     *
  -     * @param iterator The list provider.
  -     * @param separator String delimiter to separate list elements
  -     * with.
  -     * @return Text delimited list as a <code>String</code>.
  +     * Joins the elements of the provided iterator into a single string
  +     * containing the provided elements.
  +     * No delimiter is added before or after the list.
  +     * A null separator is the same as a blank String.
  +     *
  +     * @param iterator  the iterator of values to join together
  +     * @param separator  the separator character to use
  +     * @return the joined String
        */
       public static String join(Iterator iterator, String separator)
       {
  -        StringBuffer buf = new StringBuffer();
  -        while (iterator.hasNext())
  -        {
  +        if (separator == null) {
  +            separator = "";
  +        }
  +        StringBuffer buf = new StringBuffer(256);  // Java default is 16, probably too
small
  +        while (iterator.hasNext()) {
               buf.append(iterator.next());
  -            if (iterator.hasNext())
  -            {
  +            if (iterator.hasNext()) {
                   buf.append(separator);
               }
           }
           return buf.toString();
       }
   
  +    // Wrapping
  +    //--------------------------------------------------------------------------
  +    
       /**
  -     * Takes a block of text which might have long lines in it and wraps
  -     * the long lines based on the supplied wrapColumn parameter. It was
  -     * initially implemented for use by VelocityEmail. If there are tabs
  +     * Wraps a block of text to a specified line length.
  +     * <p>
  +     * This method takes a block of text, which might have long lines in it
  +     * and wraps the long lines based on the supplied wrapColumn parameter. 
  +     * It was initially implemented for use by VelocityEmail. If there are tabs
        * in inString, you are going to get results that are a bit strange,
        * since tabs are a single character but are displayed as 4 or 8
        * spaces. Remove the tabs.
        *
  -     * @param inString   Text which is in need of word-wrapping.
  -     * @param newline    The characters that define a newline.
  -     * @param wrapColumn The column to wrap the words at.
  -     * @return           The text with all the long lines word-wrapped.
  +     * @param str  text which is in need of word-wrapping
  +     * @param newline  the characters that define a newline
  +     * @param wrapColumn  the column to wrap the words at
  +     * @return the text with all the long lines word-wrapped
        */
  -
  -    public static String wrapText (String inString, String newline,
  -                                   int wrapColumn)
  +    public static String wrapText(String str, String newline, int wrapColumn)
       {
  -        StringTokenizer lineTokenizer = new StringTokenizer (
  -                inString, newline, true);
  +        StringTokenizer lineTokenizer = new StringTokenizer(str, newline, true);
           StringBuffer stringBuffer = new StringBuffer();
   
  -        while (lineTokenizer.hasMoreTokens ())
  -        {
  -            try
  -            {
  +        while (lineTokenizer.hasMoreTokens()) {
  +            try {
                   String nextLine = lineTokenizer.nextToken();
   
  -                if (nextLine.length() > wrapColumn)
  -                {
  +                if (nextLine.length() > wrapColumn) {
                       // This line is long enough to be wrapped.
  -                    nextLine = wrapLine (nextLine, newline, wrapColumn);
  +                    nextLine = wrapLine(nextLine, newline, wrapColumn);
                   }
   
  -                stringBuffer.append (nextLine);
  -            }
  -            catch (NoSuchElementException nsee)
  -            {
  +                stringBuffer.append(nextLine);
  +                
  +            } catch (NoSuchElementException nsee) {
                   // thrown by nextToken(), but I don't know why it would
                   break;
               }
  @@ -477,30 +571,25 @@
       }
   
       /**
  -     * Wraps a single line of text. Called by wrapText(). I can't
  -     * think of any good reason for exposing this to the public,
  -     * since wrapText should always be used AFAIK.
  -     *
  -     * @param line       A line which is in need of word-wrapping.
  -     * @param newline    The characters that define a newline.
  -     * @param wrapColumn The column to wrap the words at.
  -     * @return           A line with newlines inserted.
  +     * Wraps a single line of text. 
  +     * Called by wrapText() to do the real work of wrapping.
  +     *
  +     * @param line  a line which is in need of word-wrapping
  +     * @param newline  the characters that define a newline
  +     * @param wrapColumn  the column to wrap the words at
  +     * @return a line with newlines inserted
        */
  -
  -    protected static String wrapLine (String line, String newline,
  -                                      int wrapColumn)
  +    private static String wrapLine (String line, String newline, int wrapColumn)
       {
           StringBuffer wrappedLine = new StringBuffer();
   
  -        while (line.length() > wrapColumn)
  -        {
  -            int spaceToWrapAt = line.lastIndexOf (' ', wrapColumn);
  +        while (line.length() > wrapColumn) {
  +            int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn);
   
  -            if (spaceToWrapAt >= 0)
  -            {
  -                wrappedLine.append (line.substring (0, spaceToWrapAt));
  -                wrappedLine.append (newline);
  -                line = line.substring (spaceToWrapAt + 1);
  +            if (spaceToWrapAt >= 0) {
  +                wrappedLine.append(line.substring(0, spaceToWrapAt));
  +                wrappedLine.append(newline);
  +                line = line.substring(spaceToWrapAt + 1);
               }
   
               // This must be a really long word or URL. Pass it
  @@ -508,61 +597,29 @@
               // wrapColumn would allow. This behavior could be
               // dependent on a parameter for those situations when
               // someone wants long words broken at line length.
  -            else
  -            {
  -                spaceToWrapAt = line.indexOf (' ', wrapColumn);
  +            else {
  +                spaceToWrapAt = line.indexOf(' ', wrapColumn);
   
  -                if (spaceToWrapAt >= 0)
  -                {
  -                    wrappedLine.append (line.substring (0, spaceToWrapAt));
  -                    wrappedLine.append (newline);
  -                    line = line.substring (spaceToWrapAt + 1);
  -                }
  -                else
  -                {
  -                    wrappedLine.append (line);
  +                if (spaceToWrapAt >= 0) {
  +                    wrappedLine.append(line.substring(0, spaceToWrapAt));
  +                    wrappedLine.append(newline);
  +                    line = line.substring(spaceToWrapAt + 1);
  +                } else {
  +                    wrappedLine.append(line);
                       line = "";
                   }
               }
           }
   
  -        // Whatever is left in line is short enough to just pass through,
  -        // just like a small small kidney stone
  -        wrappedLine.append (line);
  +        // Whatever is left in line is short enough to just pass through
  +        wrappedLine.append(line);
   
           return (wrappedLine.toString());
       }
   
  -    /**
  -     * Uncapitalise a string. That is, convert the first character into 
  -     * lower-case.
  -     *
  -     * @param str String to uncapitalise
  -     *
  -     * @return String uncapitalised
  -     */
  -    static public String uncapitalise(String str) {
  -        return new StringBuffer(str.length())
  -            .append(Character.toLowerCase(str.charAt(0)))
  -            .append(str.substring(1))
  -            .toString();
  -    }
  -
  -    /**
  -     * Capitalise a string. That is, convert the first character into 
  -     * title-case.
  -     *
  -     * @param str String to capitalise
  -     *
  -     * @return String capitalised
  -     */
  -    static public String capitalise(String str) {
  -		return new StringBuffer(str.length())
  -			.append(Character.toTitleCase(str.charAt(0)))
  -			.append(str.substring(1))
  -			.toString();
  -	}
  -
  +    // Replacing
  +    //--------------------------------------------------------------------------
  +    
       /**
        * Replace a string with another string inside a larger string, once.
        *
  @@ -1153,6 +1210,48 @@
       }   
   
       /**
  +     * Uncapitalise a string. That is, convert the first character into 
  +     * lower-case.
  +     *
  +     * @param str String to uncapitalise
  +     *
  +     * @return String uncapitalised
  +     */
  +    static public String uncapitalise(String str) {
  +        return new StringBuffer(str.length())
  +            .append(Character.toLowerCase(str.charAt(0)))
  +            .append(str.substring(1))
  +            .toString();
  +    }
  +
  +    /**
  +     * Capitalise a string. That is, convert the first character into 
  +     * title-case.
  +     *
  +     * @param str String to capitalise
  +     *
  +     * @return String capitalised
  +     */
  +    static public String capitalise(String str) {
  +        return new StringBuffer(str.length())
  +            .append(Character.toTitleCase(str.charAt(0)))
  +            .append(str.substring(1))
  +            .toString();
  +    }
  +
  +    /**
  +     * Makes the first letter capital and leaves the rest as is.
  +     *
  +     * @param text The text to modify.
  +     * @return The modified text.
  +     */
  +    public static String firstLetterCaps(String text)
  +    {
  +        return (text == null ? null :
  +                text.substring(0, 1).toUpperCase() + text.substring(1));
  +    }
  +
  +    /**
        * Swaps the case of String. Properly looks after 
        * making sure the start of words are Titlecase and not 
        * Uppercase.
  @@ -1189,47 +1288,6 @@
       }
   
   
  -    // From .NET
  -    /**
  -     * Find the earlier index of any of a set of potential substrings.
  -     */
  -    static public int indexOfAny(String str, String[] strs) {
  -        int sz = strs.length;
  -
  -        // String's can't have a MAX_VALUEth index.
  -        int ret = Integer.MAX_VALUE;  
  -
  -        int tmp = 0;
  -        for(int i=0; i<sz; i++) {
  -            tmp = str.indexOf(strs[i]);
  -            if(tmp == -1) {
  -                continue;
  -            }
  -
  -            if(tmp < ret) {
  -                ret = tmp;
  -            }
  -        }
  -
  -        return (ret == Integer.MAX_VALUE)?-1:ret;
  -    }
  -
  -    /**
  -     * Find the latest index of any of a set of potential substrings.
  -     */
  -    static public int lastIndexOfAny(String str, String[] strs) {
  -        int sz = strs.length;
  -        int ret = -1;
  -        int tmp = 0;
  -        for(int i=0; i<sz; i++) {
  -            tmp = str.lastIndexOf(strs[i]);
  -            if(tmp > ret) {
  -                ret = tmp;
  -            }
  -        }
  -        return ret;
  -    }
  -
       /**
        * Strip any of a supplied substring from the end of a String..
        */
  @@ -1701,55 +1759,6 @@
           return str.toLowerCase();
       }
   
  -    static public String substring(String str, int start) {
  -        if(str == null) {
  -            return null;
  -        }
  -
  -        // handle negatives
  -        if(start < 0) {
  -            start = str.length() + start;    // remember start is negative
  -        }
  -
  -        if(start < 0) {
  -            start = 0;
  -        }
  -
  -        return str.substring(start);
  -    }
  -    static public String substring(String str, int start, int end) {
  -        if(str == null) {
  -            return null;
  -        }
  -
  -        // handle negatives
  -        if(end < 0) {
  -            end = str.length() + end;    // remember end is negative
  -        }
  -        if(start < 0) {
  -            start = str.length() + start;    // remember start is negative
  -        }
  -
  -        // check length next
  -        if(end > str.length()) {
  -            // check this works.
  -            end = str.length();
  -        }
  -
  -        // what if start is greater than end??
  -
  -        if(start < 0) {
  -            start = 0;
  -        }
  -
  -        // a good default?
  -        if(end < 0) {
  -            end = 0;
  -        }
  -
  -        return str.substring(start, end);
  -    }
  -
       static public String reverseDottedName(String text) {
           return reverseDelimitedString(text, ".");
       }
  @@ -1794,6 +1803,30 @@
               }
           }
           return text;
  +    }
  +
  +    /**
  +     * Get the stack trace from a Throwable as a String.
  +     * <p>
  +     * This method uses printStackTrace() internally to obtain the stack trace.
  +     *
  +     * @see java.lang.Throwable#printStackTrace()
  +     * @param throwable  the throwable to extract a stack trace from
  +     * @return the extracted stack trace, or null if an error occurs
  +     */
  +    public static String stackTrace(Throwable throwable)
  +    {
  +        String trace = null;
  +        try {
  +            // And show the Error Screen.
  +            ByteArrayOutputStream buf = new ByteArrayOutputStream();
  +            throwable.printStackTrace( new PrintWriter(buf, true) );
  +            trace = buf.toString();
  +            
  +        } catch (Exception ex) {
  +            // ignore
  +        }
  +        return trace;
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message