ProperCase class <see body>

Discussion in 'Java' started by Kabal, Sep 3, 2003.

  1. Kabal

    Kabal Guest

    Anyone have a better way to implement the below code...


    public class ProperCase {
    private ProperCase() {
    }

    public static String convert(String s) {
    char[] chars = s.trim().toLowerCase().toCharArray();
    boolean found = false;

    for (int i=0; i<chars.length; i++) {
    if (!found && Character.isLetter(chars)) {
    chars = Character.toUpperCase(chars);
    found = true;
    } else if (Character.isWhitespace(chars)) {
    found = false;
    }
    }

    return String.valueOf(chars);
    }
    }
    Kabal, Sep 3, 2003
    #1
    1. Advertising

  2. Kabal

    Will Clark Guest

    I think that is a good way of doing it... of course, if you are trying to
    optimise it, there are things you can do if you first know something about
    the characters in the string to convert...

    For example: if you know the "whitespace" is only ever a SPACE (char = 0x20)
    then only check for this instead of calling Character.isWhitespace which
    checks a whole range of different whitespace characters

    Also, for example: if you know that the string only contains the letters a
    to z, apart from the whitespace, then to get uppercase form of the letters a
    to z, just subtract 0x20 from the character... ie:

    if (!found && chars >= 'a' && chars <= 'z')
    {
    chars = chars - 0x20;
    found = true;
    }
    else if (chars == ' ')
    {
    found = false;
    }




    "Kabal" <> wrote in message
    news:...
    > Anyone have a better way to implement the below code...
    >
    >
    > public class ProperCase {
    > private ProperCase() {
    > }
    >
    > public static String convert(String s) {
    > char[] chars = s.trim().toLowerCase().toCharArray();
    > boolean found = false;
    >
    > for (int i=0; i<chars.length; i++) {
    > if (!found && Character.isLetter(chars)) {
    > chars = Character.toUpperCase(chars);
    > found = true;
    > } else if (Character.isWhitespace(chars)) {
    > found = false;
    > }
    > }
    >
    > return String.valueOf(chars);
    > }
    > }
    Will Clark, Sep 3, 2003
    #2
    1. Advertising

  3. Kabal

    Chris Berg Guest

    What's wrong with s.toUpperCase(); ??
    Chris Berg, Sep 4, 2003
    #3
  4. In article <>,
    Chris Berg <> wrote:

    > What's wrong with s.toUpperCase(); ??


    Proper Case means capitalizing the very first letter
    and the first letter after each space and no others.
    Chris Riesbeck, Sep 4, 2003
    #4
  5. Kabal

    Roedy Green Guest

    On Thu, 04 Sep 2003 13:03:45 -0500, Chris Riesbeck
    <> wrote or quoted :

    >Proper Case means capitalizing the very first letter
    >and the first letter after each space and no others.


    see http://mindprod.com/products.html#BUS

    download the business classes. In the class Misc is a method
    call something like toTitleCase or toBookcase that does what you want.

    There is also similar code in the Quoter amanuensis. See
    http://mindprod.com/quoter.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 4, 2003
    #5
  6. Kabal

    Wojtek Guest

    On 2 Sep 2003 19:00:54 -0700, (Kabal) wrote:

    >Anyone have a better way to implement the below code...


    Sorry about the weird formatting, but the word wrap munges it...

    /**
    * This method will capitalize a String.
    *
    * It is assumed that the input String is a person's name. The
    following rules are applied:
    * <ul>
    * <li>First letter is capitalized:
    * <ul>
    * <li>bill becomes Bill</li>
    * </ul>
    * </li>
    * <li>mc and mac are respected:
    * <ul>
    * <li>mcphersen becomes McPhersen</li>
    * <li>macdonald becomes MacDonald</li>
    * </ul>
    * </li>
    * </ul>
    *
    * @param theValue
    * @return String
    */
    public static String properName(String theValue)
    {
    try
    {
    return
    convertProperName(theValue.trim().toLowerCase());
    }
    catch (NullPointerException e)
    {
    return "";
    }
    }

    /**
    * This method will capitalize a String.
    *
    * It is assumed that the input String contains one or more person's
    names. The following rules are applied:
    * <ul>
    * <li>First letter of each name is capitalized:
    * <ul>
    * <li>bill smith becomes Bill Smith</li>
    * </ul>
    * </li>
    * <li>mc and mac are respected:
    * <ul>
    * <li>bill mcphersen becomes Bill McPhersen</li>
    * <li>bill macdonald becomes Bill MacDonald</li>
    * <li>bill a. macdonald becomes Bill A. MacDonald</li>
    * <li>bill a.t. macdonald becomes Bill A.T. MacDonald</li>
    * </ul>
    * </li>
    * </ul>
    *
    * @param theValue
    * @return String
    */
    public static String properNames(String theValue)
    {
    StringTokenizer names;

    try
    {
    names = new
    StringTokenizer(theValue.trim().toLowerCase());
    }
    catch (NullPointerException e)
    {
    return "";
    }

    StringBuffer out = new StringBuffer();
    boolean moreNames = false;
    String name;

    while (names.hasMoreElements())
    {
    if (moreNames)
    out.append(' ');

    name = (String) names.nextElement();

    if (name.indexOf(".") > 0) // contains a period, so
    might be one or more initials
    {
    StringTokenizer initials = new
    StringTokenizer(name, ".");

    while (initials.hasMoreElements())
    {
    name = (String)
    initials.nextElement();
    out.append(convertProperName(name));
    out.append('.');
    }
    }
    else
    out.append(convertProperName(name));

    moreNames = true;
    }

    return out.toString();
    }

    /**
    * This method actually does the proper name conversion.
    *
    * It is private because it assumes that:
    * - the passed value is not null
    * - the name has been converted to lower case
    *
    * It is used from:
    * - properName
    * - properNames
    *
    * @param theValue
    * @return
    */
    private static String convertProperName(String theValue)
    {
    char charArray[] = theValue.toCharArray();
    StringBuffer out = new StringBuffer(theValue.length() + 1);
    int nextCapital;

    // handle the special class of names such as:
    // - McDonald
    // - MacDonald
    if (theValue.startsWith("mc"))
    nextCapital = 2;
    else if (theValue.startsWith("mac"))
    nextCapital = 3;
    else
    nextCapital = -1; // no capital within name

    for (int c = 0; c < charArray.length; c++)
    if (c == 0 || c == nextCapital)

    out.append(Character.toUpperCase(charArray[c]));
    else
    out.append(charArray[c]);

    return out.toString();
    }
    ------------------------
    Wojtek Bok
    Solution Developer
    Wojtek, Sep 5, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kabal
    Replies:
    0
    Views:
    931
    Kabal
    Jul 22, 2003
  2. Mike

    Using ProperCase in ASP

    Mike, Jul 11, 2003, in forum: ASP General
    Replies:
    5
    Views:
    126
  3. Colin Steadman

    Something more than Propercase

    Colin Steadman, Dec 12, 2003, in forum: ASP General
    Replies:
    1
    Views:
    128
    Matt Smith
    Dec 12, 2003
  4. London Boy
    Replies:
    2
    Views:
    327
    Keith Bowes
    Jan 12, 2004
  5. Replies:
    5
    Views:
    205
    A. Sinan Unur
    Sep 8, 2005
Loading...

Share This Page