Solved

Converting a String arry of CAPS to Init Caps

Posted on 2004-08-23
4
705 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 119
object oriented programming comparison 5 54
servlet example issue 6 30
tomcat not starting 6 30
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

24 Experts available now in Live!

Get 1:1 Help Now