Converting Strings to Title case

Posted on 1999-11-18
Medium Priority
Last Modified: 2008-03-03
What is the best and most efficient way to convert Strings to title case?  I know java.lang.String has methods to convert to UpperCase and to LowerCase, but I am surprised that there is nothing to convert to TitleCase.
Question by:bjg
  • 2
LVL 16

Expert Comment

ID: 2217835
Could you explain what you mean with Title case? The Latin character set has two representations for each character, one is lower case (a) one is uppercase (A). What is Title case?

Author Comment

ID: 2217860
Where the first letter of the String is in upper case and the rest are in lower case.  java.lang.Character has a method called isTitleCase().
LVL 16

Accepted Solution

imladris earned 300 total points
ID: 2218093
There is no method provided for that. You would have to write it yourself. Something along the lines of:

String tgt="THIS IS A TEST";
StringBuffer s=new StringBuffer(tgt.toLowerCase());
boolean raise=true;
for(int i=0; i<s.length(); ++i)
{   if(raise && !Character.isSpace(s.charAt(i)))
    {   s.setCharAt(i,Character.toUpperCase(s.charAt(i)));
    else if(!raise && Character.isSpace(s.charAt(i)))
    {   raise=true;
String ns=new String(s);

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month3 days, 3 hours left to enroll

598 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