How can I make only the first character of a String uppercase and the remaining lowercase?

Posted on 2011-10-08
Last Modified: 2012-05-12
How can I make only the first letter of a String uppercase and keep the remaining characters lowercase? For example: "BlAh", "bLah", "bLAH" will become "Blah"

Is there anything in the Java libraries for this?
Question by:Eindoofus
    LVL 86

    Expert Comment

    You could do
    static public String capitalize(String s) {
    		return Character.toUpperCase(s.charAt(0)) + s.substring(1);

    Open in new window


    Author Comment

    Thanks, and what command do I use for the remaining characters in s.substring to make them lower case?
    LVL 86

    Accepted Solution

    You can do
    return Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase();

    Open in new window

    LVL 86

    Expert Comment


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

    754 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