如何用Crystal Syntax实现保留指定大写缩写的首字母大写格式?
Absolutely! You can pull this off in Crystal Syntax—we’ll just need to pair the ProperCase() function with either a Case Statement (for targeted abbreviation preservation) or string manipulation logic to handle parentheses. Let’s walk through two practical approaches:
1. Targeted Abbreviation Preservation with Case Statement
If you only need to keep specific abbreviations (like ABC, XYZ) uppercase, a Case Statement paired with Replace() works perfectly. This lets you explicitly define which terms to fix after applying ProperCase().
Here’s a sample formula:
// Store the original database value local stringvar original_text := {YourDatabaseTable.YourColumnName}; // First convert the entire string to proper case local stringvar proper_cased := ProperCase(original_text); // Use Case Statement to replace specific proper-cased abbreviations back to uppercase proper_cased := Case When InStr(proper_cased, "(Abc)") > 0 Then Replace(proper_cased, "(Abc)", "(ABC)") When InStr(proper_cased, "(Xyz)") > 0 Then Replace(proper_cased, "(Xyz)", "(XYZ)") // Add more lines here for additional abbreviations you need to preserve Else proper_cased End; // Output the final formatted string proper_cased;
This checks if the proper-cased string contains your target abbreviations (in their lowercase-first form) and replaces them with the uppercase version.
2. Universal Parentheses Content Preservation
If you want all text inside parentheses to stay uppercase (not just specific abbreviations), we can use string position functions to locate parentheses and convert their contents to uppercase. This is more flexible if you have multiple unknown abbreviations in parentheses:
local stringvar original_text := {YourDatabaseTable.YourColumnName}; local stringvar final_text := ProperCase(original_text); local numbervar open_paren_pos := InStr(final_text, "("); local numbervar close_paren_pos := InStr(final_text, ")"); // Handle the first set of parentheses if open_paren_pos > 0 and close_paren_pos > open_paren_pos then local stringvar abbr_text := Mid(final_text, open_paren_pos + 1, close_paren_pos - open_paren_pos - 1); final_text := Replace(final_text, "(" & abbr_text & ")", "(" & UCase(abbr_text) & ")"); end if; // Loop to handle multiple sets of parentheses (if needed) open_paren_pos := InStr(close_paren_pos + 1, final_text, "("); close_paren_pos := InStr(close_paren_pos + 1, final_text, ")"); do while open_paren_pos > 0 and close_paren_pos > open_paren_pos local stringvar abbr_text := Mid(final_text, open_paren_pos + 1, close_paren_pos - open_paren_pos - 1); final_text := Replace(final_text, "(" & abbr_text & ")", "(" & UCase(abbr_text) & ")"); open_paren_pos := InStr(close_paren_pos + 1, final_text, "("); close_paren_pos := InStr(close_paren_pos + 1, final_text, ")"); loop; final_text;
This code first converts the whole string to proper case, then finds any parentheses, extracts their content, converts it to uppercase, and replaces the proper-cased version back into the string. The loop handles cases where there are multiple parenthetical phrases.
内容的提问来源于stack exchange,提问作者kazzi

