Solved

String conversion from UPPERCASE to Title Case

Posted on 2004-08-03
14
2,099 Views
Last Modified: 2008-01-09
I'm very new to JSP, i'm in a situation where i have to convert UPPERCASE string to Title Case.

Is there any Java function for this? or any other way to perform this job.
0
Comment
Question by:adnanj76
  • 7
  • 6
14 Comments
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Actually that same thing is now part of the jakarta taglibs:

http://jakarta.apache.org/taglibs/

it's the "String" one

Actually that is a taglib wrapper round the Apache Commons "lang" package

http://jakarta.apache.org/commons/lang/

org.apache.commons.lang.WordUtils.capitalizeFully( String s )

Tim
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
If Tim have already answer your question, skip this comment.

>>UPPERCASE string to Title Case.
Could you give me and example?

"UPPER CASE" --> UpperCase

Do you mean something like this?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
No, like:

"THIS IS A COOL STRING"

to

"This Is A Cool String"

I think :-)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
The code in Apache commons is:
----------------------
    /**

     * <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();

    }
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Bizzarre double spaces...  sorry about that... :-(
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
If so, you can use this code.

TitleCase.jsp
========
<%!
private String toTitleCase(String str) {
      String[] tokens = str.split(" ");  
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<tokens.length; i++) {
          sb.append(tokens[i].substring(0,1).toUpperCase());
          sb.append(tokens[i].toLowerCase().substring(1));
        }
        return sb.toString();
}
%>
<html>
<head>
<title>TitleCase</title>
</head>
<body bgcolor="#ffffff">
<%=toTitleCase("THIS IS THE TEST.")%>
</body>
</html>
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
If you mean like Tim, this may be shorter

TitleCase.jsp
========
<%!
private String toTitleCase(String str) {
      String[] tokens = str.split(" ");  
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<tokens.length; i++) {
          sb.append(tokens[i].substring(0,1).toUpperCase());
          sb.append(tokens[i].toLowerCase().substring(1));
          sb.append(" ");
        }
        return sb.toString();
}
%>
<html>
<head>
<title>TitleCase</title>
</head>
<body bgcolor="#ffffff">
<%=toTitleCase("THIS IS THE TEST.")%>
</body>
</html>
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
I think Character.toTitleCase() may be better than toUpperCase() not sure though
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
Yes. Tim. You're right.
For performance purpose. If we have a very long string, we can see the different.
But If this application is just showing the string legth < 100 character, I think we could not feel the difference.

I just want to share idea. :)
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
Your code have a good performance while my code have a good readability. I think so. :)
0
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 125 total points
Comment Utility
I think the time that this code use compare to the time that SQL query making. I think it is very different.
toTitleCase may use 10 ms while statement.executeQuery may use 10 sec.

Anyway your code look well.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Did I not help at all then adnanj76  :-(

Sorry about that...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now