File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to convert string to mixed case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How to convert string to mixed case" Watch "How to convert string to mixed case" New topic
Author

How to convert string to mixed case

Munish Dabra
Greenhorn

Joined: Nov 18, 2003
Posts: 19
Is there any api which take the string and convert it to mixed case. If somthing is there in commons..that will work too.

Thanks
Munish


Munish Dabra<br />SCJP , SCWD
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Can you give an example of a mixed case format you want to use?
[ November 29, 2005: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Munish Dabra
Greenhorn

Joined: Nov 18, 2003
Posts: 19
basically sentence case

e.g. hI thIS iS StUPId QuesTIon

Output : This is stupid question
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, the following does it (if you first check that the String isn't null and is at least two characters long

s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase()

I'm sure someone could come up with a somewhat more efficient way to do it, but I doubt there's anything simpler.


[Jess in Action][AskingGoodQuestions]
Munish Dabra
Greenhorn

Joined: Nov 18, 2003
Posts: 19
i code like this , but didnt quite like it..any straithword commons api or better way to do it ?

if(str != null){
String[] sentences = StringUtils.split(str.toLowerCase(),'.');
for(int i=0;i<sentences.length;i++){
StringBuffer formattedSentence = new StringBuffer();
formattedSentence.append(StringUtils.left(sentences[i],1).toUpperCase());
formattedSentence.append(str.substring(1));
sentences[i] = formattedSentence.toString();
}
return StringUtils.join(sentences,'.');
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
EFH: That works for a one-character input, too.
[ November 29, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So the rules are:

1. The first letter must be upper-case.

2. The first letter after '.' must be upper-case.

3. All other letters must be lower-case.

If so, then something like this: (not tested)
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

Greetings All,

Your replies all seem to give a result, but i don't see how the same input, will give the same output with your code.

As i see it this assumes that there was a typo (or at least misrepresentation) of the test case (example).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Right, this assumes Munish merely forgot about the "hI " at the beginning of the original input.
Munish Dabra
Greenhorn

Joined: Nov 18, 2003
Posts: 19
yes guys , there was a little typo in the example.And final piece i think should be :

if(str != null && !str.trim().equalsIgnoreCase("")){
String[] sentences = StringUtils.split(str.trim().toLowerCase(),'.');
for(int i=0;i<sentences.length;i++){
sentences[i] = StringUtils.left(sentences[i].trim(),1).toUpperCase().concat(sentences[i].trim().substring(1));
}
return StringUtils.join(sentences,".").concat(".");
}
any comments !
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Anyone have a solution that uses java regular expressions? Just wondering, I don't use them enough to know for sure whether or not such a thing could be done.

To Munish: What about special words like a person's name, book title, important words, acronyms, etc. If you need to include any of those, formatting becomes much more difficult.
[ November 30, 2005: Message edited by: Scott Selikoff ]
Munish Dabra
Greenhorn

Joined: Nov 18, 2003
Posts: 19
True Scott , my code does n't take care of the special words like a person's name, book title, important words, acronyms etc.

But I couldn't think of anything to do that . Any Ideas ?
 
wood burning stoves
 
subject: How to convert string to mixed case