| 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
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
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: 24040
|
|
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: 18670
|
|
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: 16479
|
|
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
Ranch Hand
Joined: Sep 18, 2000
Posts: 491
|
|
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: 18670
|
|
|
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
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
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 ?
|
 |
 |
|
|
subject: How to convert string to mixed case
|
|
|