Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capitalization Formula 2

Status
Not open for further replies.

psamedy

Technical User
Apr 22, 2002
60
US
Hello All

I'm looking for sample code that will help me with capitalizing the first letter of every word in a string -similar to the built in Leadingcaps(x) function - but to include exceptions. For example using leadingcaps a string that may originally read GOLDBERG AND GOLDBERG now reads Goldberg And Goldberg when i need it to read Goldberg and Goldberg. So i need it to work on conditions. More examples are SUITE 1CB prints Suite 1cb when i'd like to read Suite 1CB. Is there a solution to this? Thanks Pat
 
Hi
try this
left({table.field}, 1) + lcase(right({table.field}, len({table.field}) - 1))

cheers

pgtek
 
Assuming Crystal 8.5, you can use pgtek's formula as a beginning. This gets you "Goldberg and goldberg", of course

Then do replaces for every letter of the alphabet,
Replace (Your_field, " g", " G")
This will give "Goldberg And Goldberg". You'll have to do a lot of replaces, though it's OK to nest a replace within a replace, and use the output of one formula field as the input to the next.

Finally replace the erronious title capitals,
Replace (Your_field, " And ", " and ")

P.S. What is Leadingcaps(x)? Is it from another language? Because I can't find it in Crystal

Madawc Williams
East Anglia, Great Britain
 
LeadingCaps is a Crystal function, later called ProperCase in v9.

If you're writing formulas with approaches like writing a Replace command for every letter in the alphabet, I would suggest you download the LeadingCaps UFL from the Crystal support site, which makes this kind of severe overkill unnecessary. If you can't do that, at least think about using a loop to capitalise the letters you want.

LeadingCaps / ProperCase / InitCap, depending on your system, are subjective commands, which cannot guarantee a 100% success rate.
Code:
WhilePrintingRecords;
StringVar x := "GOLDBERG AND GOLDBERG";

x := Replace(LeadingCaps(x),' And ',' and ');

x;
This formula will work in the instance you've supplied above, but you will have to intervene if you want to explain that MFI is supposed to be capitalised, or eBay is supposed to be capitalised in that strange inverse manner.

If you want to stop specific instances from being ProperCased, you might find that you have to enter a clause in your formula like &quot;If (your field) in {@FormulaX} then (your field) else <enter replace leading caps clause>&quot;.

@FormulaX would be a repository of business names that you'd want excluded:
e.g.
&quot;MFI&quot;
&quot;EBAY&quot;
&quot;AT&T&quot;

Hard coding is never a great idea, but when you're trying to hone LeadingCaps, the only way you're going to get even close to 100% success is with human intervention.

Naith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top