Crystal Reports Proper Case函数高级求助:地址8Th需修正为8th
Got it, let's tackle that frustrating address formatting issue! The built-in ProperCase function works perfectly for names, but it doesn't know to fix ordinal suffixes like "8Th" → "8th"—which definitely looks unprofessional for employee-facing docs. Here are two reliable solutions tailored to Crystal Reports:
1. Custom Formula for One-Off Use
This formula first applies proper case to your address, then scans for digits followed by miscapitalized ordinal suffixes (St/Nd/Rd/Th) and fixes them without breaking other words (like "Thomas" or "Third").
Create a new formula field and paste this code, replacing {YourTable.AddressField} with your actual address field:
Local StringVar formattedAddr := ProperCase({YourTable.AddressField}); Local NumberVar position := 1; While position <= Length(formattedAddr) Do ( // Check if current character is a digit If Mid(formattedAddr, position, 1) Like "#" Then ( // Make sure we don't go out of bounds checking the suffix If position + 2 <= Length(formattedAddr) Then ( Select Mid(formattedAddr, position + 1, 2) Case "St": formattedAddr := Replace(formattedAddr, Mid(formattedAddr, position + 1, 2), "st"); Case "Nd": formattedAddr := Replace(formattedAddr, Mid(formattedAddr, position + 1, 2), "nd"); Case "Rd": formattedAddr := Replace(formattedAddr, Mid(formattedAddr, position + 1, 2), "rd"); Case "Th": formattedAddr := Replace(formattedAddr, Mid(formattedAddr, position + 1, 2), "th"); ; // Skip ahead past the digit and suffix to avoid reprocessing position := position + 3; ) Else ( position := position + 1; ) ) Else ( position := position + 1; ) ); formattedAddr
How it works:
- Starts by standardizing the address to proper case (so "8TH AVE" becomes "8Th Ave")
- Loops through each character to find digits, then checks the next two characters for common ordinal suffixes
- Replaces miscapitalized suffixes with lowercase versions, leaving other words (like "Thornhill" or "St. Louis") untouched
2. Reusable Custom Function
If you need to apply this fix to multiple address fields across your report, turn the logic into a reusable custom function:
- Go to Field Explorer > Functions > Custom Functions > New
- Name it
FixOrdinalProperCase - Add a string parameter (e.g.,
inputString) - Paste the formula code above, replacing
ProperCase({YourTable.AddressField})withProperCase(inputString) - Save the function
Now you can call it anywhere in your report like this:
FixOrdinalProperCase({YourTable.ShippingAddress})
Quick Notes:
- This handles all common ordinal suffixes (1st, 2nd, 3rd, 4th-9th) and works with multi-digit numbers too (e.g., "101St" → "101st")
- If you encounter edge cases (like suffixes separated by a space, though that's rare in addresses), you can adjust the loop logic to check for spaces after digits
内容的提问来源于stack exchange,提问作者Amanda Lampe

