• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
      • Introduction to CSPro Language
      • Data Requirements
      • CSPro Program Structure
      • Programming Standards
      • Code Folding
      • Debugging CSPro Applications
      • Declaration Section
      • Procedural Sections
      • Logic
      • Language Elements
        • Version
        • Delimiters
        • Comments
        • Preprocessor
        • Variables and Constants
        • Expressions
          • Expressions
          • Substring Expressions
        • Operators
        • Files
        • Miscellaneous
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

Substring Expressions

Format
string_variable[start_index:string_length]
Description
A substring expression lets you extract a part (substring) of a string. The start_index gives the starting character position of the substring within the string, and string_length gives the number of characters to include in the substring, including the starting character. If string_length is not given, then it is assumed to be to the end of the originating string. A negative start_index leads to the basing of the substring from the end of the string.
Example 1
Suppose the variable STR has the value "ABCDEF":
STR[1]      "ABCDEF"
STR[
3:1]    "C"
STR[
3]      "CDEF"
STR[
2:3]    "BCD"
STR[
5]      "EF"
STR[
4:7]    "DEF"
STR[-
2]     "EF"
STR[-
4:2]   "CD"
Both start_index and string_length can be numeric expressions as well as constants. For example, to obtain the last 3 characters of STR you could use the expression:
STR[length(STR) - 2:3]
In this example, if STR is not at least two characters long, you may get unexpected results. You could also write the above as:
STR[-3]
Example 2
Likewise, substring expressions can be performed on string arrays. Suppose the string array crop had the following definition:
PROC GLOBAL

Array string crop(5); // 5 crop names

PROC MY_PROGRAM

preproc

    crop(
1) = "maize";
    crop(
2) = "wheat";
    crop(
3) = "rice";
    crop(
4) = "potatoes";
    crop(
5) = "legumes";
The following substring expressions would yield the results as shown:
crop(1)[2]      "aize"
crop(
1)[3:1]    "i"
crop(
2)[3]      "eat"
crop(
3)[2]      "ice"
crop(
4)[5]      "toes"
crop(
5)[1:3]    "leg"