• <Helps for GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • CSPro Statements and Functions
      • Statement Format Symbols
      • Alphabetical List of Functions and Statements
      • List of Reserved Words
      • Deprecated Features
      • Declaration Statements
      • Array Object
      • Audio Object
      • Barcode and QR Codes
      • Document Object
      • File Object
      • Freq Object
      • Geometry Object
      • HashMap Object
      • Image Object
      • List Object
      • Map Object
      • Path
      • Pff Object
      • SystemApp Object
      • ValueSet Object
      • Program Control Statements
      • Assignment Statements
      • Data Entry Statements and Functions
      • Batch Edit Statements
      • Numeric Functions
      • String Functions
      • Multiple Occurrence Functions
      • General Functions
        • Compress Function
        • Decompress Function
        • Diagnostics Function
        • Encode Function
        • ErrMsg Function
        • ExecSystem Function (Desktop)
        • ExecSystem Function (Mobile)
        • ExecPFF Function
        • GetProperty Function
        • GetLabel Function
        • GetLanguage Function
        • GetSymbol Function
        • GetValueLabel Function
        • Hash Function
        • HtmlDialog Function
        • InValueSet Function
        • Invoke Function
        • IsChecked Function
        • LoadSetting Function
        • LogText Function
        • MaxValue Function
        • MinValue Function
        • Paradata Function
        • PathConcat Function
        • PathName Function
        • Report Function (Deprecated)
        • SaveSetting Function
        • SetLanguage Function
        • SetProperty Function
        • SetReportData Function (Deprecated)
        • SetValueSet Function
        • SetValueSet Function With Arrays
        • SetValueSets Function
        • Special Function
        • SqlQuery Function
        • Stop Function
        • SysParm Function
        • Tr Function
        • Trace Function
        • UUID Function
        • View Function
        • Warning Function
      • Date and Time Functions
      • External File Functions
      • Synchronization Functions
      • Export Attributes
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Appendix
  • <Helps for CSEntry>
  • <Helps for CSBatch>
  • <Helps for CSTab>
  • <Helps for DataViewer>
  • <Helps for TextView>
  • <Helps for TblView>
  • <Helps for TRSWin>
  • <Helps for CSDeploy>
  • <Helps for CSPack>
  • <Helps for CSFreq>
  • <Helps for CSSort>
  • <Helps for CSExport>
  • <Helps for CSReFmt>
  • <Helps for CSDiff>
  • <Helps for CSConcat>
  • <Helps for TRSSetup>
  • <Helps for ParadataViewer>
  • <Helps for ParadataConcat>
  • <Helps for CSIndex>
  • <Helps for Excel2CSPro>
  • <Helps for CSWeb>

Encode Function

Format
s = encode(ʃencoding_type,ʅ text);
Description
The encode function escapes special characters to facilitate writing to HTML or CSV files, or creates properly encoded strings for a variety of formats.
The optional argument encoding_type specifies the format for encoding. The text argument is a string expression that specifies the text to encode. If an encoding type is provided with no text, then the default encoding type for the application is modified. An application starts with the default encoding type set to HTML.
The encoding types are:
Encoding TypeDescription
HTMLEncodes the characters < > & and newlines for writing to HTML files.
CSVEncodes the characters , " and newlines for writing to comma-separated values files.
URIEncodes a group of characters for writing uniform resource identifiers (URIs).
URIComponentEncodes a group of characters for writing the components of URIs.
PercentEncodingEncodes a group of characters for writing to formats such as connection strings.
JsonStringEncodes text for writing as a valid JSON string.
SlashesAdds backslashes to escape quote and backslash characters.
Return Value
The function returns a string with the appropriate characters encoded. If the default encoding type is changed, the function returns a blank string.
Example
// write to a HTML file
file html_file;

html_file.write("<p>Enumerator name: %s</p>", encode(strip(ENUMERATOR_NAME)));

string house_image_filename = pathconcat("../House Images", key(HOUSING_DICT) + ".jpg");
html_file.write("<p><img src='file:///%s' alt='House' /></p>", encode(URI, house_image_filename));


// write to a CSV file
file csv_file;

encode(CSV);
csv_file.write("Address,Head of Household");
csv_file.write("%s,%s", encode(strip(ADDRESS)), encode(strip(HEAD_NAME)));
See also: File.Write Function, Replace Function