Solved

how to captalize the first letter of a String

Posted on 2006-07-03
5
2,709 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

Industry Leaders: 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

Title # Comments Views Activity
Struts tiles for layout structure along with Angular JS. 4 183
maven webservice projet error 6 80
maven set up 2 207
servlet example 17 69
Configuring Remote Assistance for use with SCCM
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

739 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