Solved

Converting a String arry of CAPS to Init Caps

Posted on 2004-08-23
4
702 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
oops:

System.out.println(ret);
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

744 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