Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert string to mixed case

 
Munish Dabra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give an example of a mixed case format you want to use?
[ November 29, 2005: Message edited by: Scott Selikoff ]
 
Munish Dabra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically sentence case

e.g. hI thIS iS StUPId QuesTIon

Output : This is stupid question
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Munish Dabra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EFH: That works for a one-character input, too.
[ November 29, 2005: Message edited by: Jim Yingst ]
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, this assumes Munish merely forgot about the "hI " at the beginning of the original input.
 
Munish Dabra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic