Solved

Converting a String arry of CAPS to Init Caps

Posted on 2004-08-23
4
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 59
printf performancy 11 86
jsp CRUD operations with and without prepared statement also hibernatge 1 31
import as existing maven project 3 41
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 about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

726 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