• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting a String to titlecase but ignoring a specific word  RSS feed

 
Dave Nice
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I need to turn some strings from:
'1-the-high-street'
to:
'1 The High Street'

But...
if string is
'the-dog-and-duck'
I want to achieve
'The Dog and Duck'
...whereby the 'and' is NOT converted to title case

My code as follows:

String propertyPageTitle = "1-the-high-street";
propertyPageTitle = propertyPageTitle.replace("-", " ");
propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);
System.out.println(propertyPageTitle);

prints: '1 The High Street'

How could I handle the second scenario so it does not convert 'and' to titlecase?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you clarify the rule a bit clearly in a statement? On what basis is poor and not honoured with title case?
 
Dave Nice
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:Can you clarify the rule a bit clearly in a statement? On what basis is poor and not honoured with title case?


this is not important. I just need to know how
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Nice wrote:this is not important. I just need to know how

I guess you didn't take my comment seriously. Anyways, if it's not important, then you can just do a replace like this:

I know you wouldn't be satisified, that is why I asked, we need some set of rules to identify the words that shouldn't be converted to title case.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Nice wrote:this is not important. I just need to know how

So, tell US - and I suspect that in doing so, you'll have worked out all you need to code the rules (and there are rules).

Specificaly: What makes '1-the-high-street' different from 'the-dog-and-duck', and why shouldn't you capitalize 'and'?

Until YOU understand WHY, we certainly can't tell you HOW.

Winston
 
Xander Jacob
Greenhorn
Posts: 13
Java Netbeans IDE Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could split the String into words:

and check each substring (The stuff between every hyphen. All hyphens are destroyed. Sorry if you had an attachment to them or something...) to see if it equals "and". I'm going off the rule that every "and" does not deserve to be capitalized.

And your capitalize method is pretty long because Strings are immutable and it's hard to manipulate stuff that cannot be manipulated.



THERE! You now have your selective capitalizer... thingy.
DISCLAIMER! I have NOT tested any of this code at all. Some of it I just found on a Javadoc and put in here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!