Solved

how to captalize the first letter of a String

Posted on 2006-07-03
5
2,637 Views
Last Modified: 2011-10-03
I get a sessioh variable in my JSP, which I can save it as a String.
Now what I need is to capitalize the first letter of the variable before displaying it.

Any help will be ggreatly appreciated

Thanks
0
Comment
Question by:huzefaq
5 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 17032352
try like:

<%
      String a = session.getAttribute("myvalue")==null?"":session.getAttribute("myvalue").toString();
      String b = a.length() > 1?  a.substring(0,1).toUpperCase() + a.substring(1).toLowerCase() : "";
      
      out.println(b);
%>
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17035126
Hi huzefaq,

Using JSTL (can be obtained from jakarta.apache.org) its very easy for string operations on jsp pages.

chk this link

http://jakarta.apache.org/taglibs/doc/string-doc/index.html


capitalize  =>       Capitalize a String. The first character is converted to TitleCase.

0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 17035517
from my example above, like:

String b = a.length() > 1?  a.substring(0,1).toUpperCase() + a.substring(1).toLowerCase() : "";

should be as:

String b = a.length() > 1?  a.substring(0,1).toUpperCase() + a.substring(1).toLowerCase() : a.toUpperCase();
0
 
LVL 2

Expert Comment

by:pankilmpatel
ID: 17040105
Hi,
There are two posibilities.
1) you want only first word to be capitalized

static void printCapitalized( String inputWord) {

String firstLetter = inputWord.substring(0,1);  // Get first letter
String remainder   = inputWord.substring(1);    // Get remainder of word.
String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();

System.out.println(capitalized);

}

2) each  word of string to be capitalized

  static void printCapitalized( String str ) {
          char ch;       // One of the characters in str.
          char prevCh;   // The character that comes before ch in the string.
          int i;         // A position in str, from 0 to str.length()-1.
          prevCh = '.';  // Prime the loop with any non-letter character.
          for ( i = 0;  i < str.length();  i++ ) {
             ch = str.charAt(i);
             if ( Character.isLetter(ch)  &&  ! Character.isLetter(prevCh) )
                 System.out.print( Character.toUpperCase(ch) );
             else
                 System.out.print( ch );
             prevCh = ch;
          }
          System.out.println();
       }

Regards,
Pankil Patel
0
 
LVL 5

Expert Comment

by:muktajindal
ID: 17072046
try:

<%
     String display = session.getAttribute("value")==null?"":session.getAttribute("value").toString();
     if (Character.isLowerCase(display.charAt(0)))
         display = display.substring(0,1).toUpperCase() + display.substring(1,display.length());
%>
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

Title # Comments Views Activity
maven project import to eclipse problems 13 102
logging jar 1 106
Can't access image with tomcat/html 6 95
eclipse luna javeEE perspecive missing 5 97
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
An analysis of the phishing scam that has been affecting Google users, along with steps to take for protection, as well as what to do if you receive one of the emails.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

20 Experts available now in Live!

Get 1:1 Help Now