• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to convert string to mixed case

 
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
 
author
Posts: 4107
28
Google Web Toolkit Eclipse IDE Flex
  • 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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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,'.');
}
 
Wanderer
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 ]
 
Marshal
Posts: 24594
55
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)
 
Bartender
Posts: 612
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
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
Posts: 4107
28
Google Web Toolkit Eclipse IDE Flex
  • 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
Boost this thread!