?
Solved

String conversion from UPPERCASE to Title Case

Posted on 2004-08-03
14
Medium Priority
?
2,207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11704082
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11704381
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
ID: 11705293
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Expert Comment

by:TimYates
ID: 11705317
No, like:

"THIS IS A COOL STRING"

to

"This Is A Cool String"

I think :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11705327
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
ID: 11705332
Bizzarre double spaces...  sorry about that... :-(
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11705396
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
ID: 11705438
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
ID: 11705469
I think Character.toTitleCase() may be better than toUpperCase() not sure though
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11705591
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
ID: 11705613
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 500 total points
ID: 11705657
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
ID: 12517229
Did I not help at all then adnanj76  :-(

Sorry about that...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you an Exchange administrator employed with an organization? And, have you encountered a corrupt Exchange database due to which you are not able to open its EDB file. This article will explain all the steps to repair corrupt Exchange database.
The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

719 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