Solved

String conversion from UPPERCASE to Title Case

Posted on 2004-08-03
14
2,117 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web service Rest Client creation 4 87
how to exclude a file using regex 5 118
withoutString  challenge 40 198
JavaScript parse form with colon separated values 2 77
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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