Solved

how to captalize the first letter of a String

Posted on 2006-07-03
5
2,727 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
[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
5 Comments
 
LVL 52

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 52

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

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

Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
Make the most of your online learning experience.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

687 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