Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting a String arry of CAPS to Init Caps

Posted on 2004-08-23
4
Medium Priority
?
712 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

722 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