Solved

String conversion from UPPERCASE to Title Case

Posted on 2004-08-03
14
2,111 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

23 Experts available now in Live!

Get 1:1 Help Now