?
Solved

how to captalize the first letter of a String

Posted on 2006-07-03
5
Medium Priority
?
2,781 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 53

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 53

Accepted Solution

by:
Ryan Chong earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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