Solved

how to captalize the first letter of a String

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

15 Experts available now in Live!

Get 1:1 Help Now