Solved

Converting a String arry of CAPS to Init Caps

Posted on 2004-08-23
4
706 Views
Last Modified: 2011-09-20
hi Experts,
                What is the most efficient way to convert a string Array of all CAPS to init Caps..That is the first letter of each word {greater than 3 Characters ..} is capitalised  But the starting alphabet of the stirng is always capitalixed.. irrepective of whether it is 3 characters long or not.... For example..

1) I AM THE STRING WHICH IS TO BE CAPITALIZED..

should be converted into

Output : I am the String Which is to be Capitalized.

What is the most efficient way to do this..

Pls give me the most efficient way to approach this .

Thanx in Advance.

Regards,
newBie Programmer
0
Comment
Question by:FearFactor_x
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11871231
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)));
        raise=false;
    }
    else if(!raise && Character.isSpace(s.charAt(i)))
    {   raise=true;
    }
}
String ns=new String(s);
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11871238
That was from http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10235434.html

There is also a class in the Apache Commons libraries to do this...  TextUtils class I think (but I can't be 100% sure)
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 11871319
and if you want it to do it so that it doesn't capitalize words 3 chars and under:

String tgt = "THIS IS A TEST";
StringTokenizer st = new StringTokenizer(tgt, " ");

String tmp;
String ret = "";
while (st.hasMoreTokens()) {
   tmp = st.nextToken();
   if (tmp.length() > 3) {
     ret += Character.toUpperCase(tmp.charAt(0)) + tmp.substring(1).toLowerCase() + " ";
   } else {
     ret += tmp.toLowerCase() + " ";
   }
}
System.out.println(tmp);
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 11871337
oops:

System.out.println(ret);
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 48
github account with ecipse 1 47
Windows 10 IE Certificate Issue 10 45
Desingning Refactoring existing code 2 24
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

813 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

17 Experts available now in Live!

Get 1:1 Help Now