You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/08/18 01:06:11 UTC

cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang WordUtils.java

scolebourne    2003/08/17 16:06:11

  Modified:    lang/src/test/org/apache/commons/lang WordUtilsTest.java
               lang/src/java/org/apache/commons/lang WordUtils.java
  Log:
  Add capitalizeFully() to deal with a whole string whatever its current state
  
  Revision  Changes    Path
  1.3       +14 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/WordUtilsTest.java
  
  Index: WordUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/WordUtilsTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- WordUtilsTest.java	17 Aug 2003 22:56:11 -0000	1.2
  +++ WordUtilsTest.java	17 Aug 2003 23:06:11 -0000	1.3
  @@ -210,6 +210,19 @@
           assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123") );
       }
       
  +    public void testCapitalizeFully_String() {
  +        assertEquals(null, WordUtils.capitalizeFully(null));
  +        assertEquals("", WordUtils.capitalizeFully(""));
  +        assertEquals("  ", WordUtils.capitalizeFully("  "));
  +        
  +        assertEquals("I", WordUtils.capitalize("I") );
  +        assertEquals("I", WordUtils.capitalize("i") );
  +        assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am here 123") );
  +        assertEquals("I Am Here 123", WordUtils.capitalizeFully("I Am Here 123") );
  +        assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am HERE 123") );
  +        assertEquals("I Am Here 123", WordUtils.capitalizeFully("I AM HERE 123") );
  +    }
  +    
       public void testUncapitalize_String() {
           assertEquals(null, WordUtils.uncapitalize(null));
           assertEquals("", WordUtils.uncapitalize(""));
  
  
  
  1.3       +44 -2     jakarta-commons/lang/src/java/org/apache/commons/lang/WordUtils.java
  
  Index: WordUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/WordUtils.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- WordUtils.java	17 Aug 2003 22:56:11 -0000	1.2
  +++ WordUtils.java	17 Aug 2003 23:06:11 -0000	1.3
  @@ -256,7 +256,8 @@
       //-----------------------------------------------------------------------
       /**
        * <p>Capitalizes all the whitespace separated words in a String.
  -     * Only the first letter of each word is changed.</p>
  +     * Only the first letter of each word is changed. To change all letters to
  +     * the capitalized case, use {@link #capitalizeFully(String)}.</p>
        *
        * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
        * A <code>null</code> input String returns <code>null</code>.
  @@ -271,6 +272,7 @@
        * 
        * @param str  the String to capitalize, may be null
        * @return capitalized String, <code>null</code> if null String input
  +     * @see #capitalizeFully(String)
        */
       public static String capitalize(String str) {
           int strLen;
  @@ -289,6 +291,46 @@
                   whitespace = false;
               } else {
                   buffer.append(ch);
  +            }
  +        }
  +        return buffer.toString();
  +    }
  +
  +    /**
  +     * <p>Capitalizes all the whitespace separated words in a String.
  +     * All letters are changed, so the resulting string will be fully changed.</p>
  +     *
  +     * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
  +     * A <code>null</code> input String returns <code>null</code>.
  +     * Capitalization uses the unicode title case, normally equivalent to
  +     * upper case.</p>
  +     *
  +     * <pre>
  +     * WordUtils.capitalize(null)        = null
  +     * WordUtils.capitalize("")          = ""
  +     * WordUtils.capitalize("i am FINE") = "I Am Fine"
  +     * </pre>
  +     * 
  +     * @param str  the String to capitalize, may be null
  +     * @return capitalized String, <code>null</code> if null String input
  +     */
  +    public static String capitalizeFully(String str) {
  +        int strLen;
  +        if (str == null || (strLen = str.length()) == 0) {
  +            return str;
  +        }
  +        StringBuffer buffer = new StringBuffer(strLen);
  +        boolean whitespace = true;
  +        for (int i = 0; i < strLen; i++) {
  +            char ch = str.charAt(i);
  +            if (Character.isWhitespace(ch)) {
  +                buffer.append(ch);
  +                whitespace = true;
  +            } else if (whitespace) {
  +                buffer.append(Character.toTitleCase(ch));
  +                whitespace = false;
  +            } else {
  +                buffer.append(Character.toLowerCase(ch));
               }
           }
           return buffer.toString();
  
  
  

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