Solved

String conversion from UPPERCASE to Title Case

Posted on 2004-08-03
14
2,154 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 125 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Invest in your employees with these five simple steps to improve employee engagement and retention.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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