%SUBSTRING 內建函數
子字串內建函數 (%SUBSTRING 或 %SST) 產生的字串是現有字串的子集。
在 Change Variable (CHGVAR) 指令中,可以指定 %SST 函數來取代要變更的變數 (VAR 參數) 或要變更變數的值 (VALUE 參數)。 在 If (IF) 指令中,可以在表示式中指定 %SST 函數。
%SUBSTRING(character-variable-name starting-position length)
它也可以格式化,如下列範例所示:
%SST(character-variable-name starting-position length)
您可以撰寫 *LDA 來取代字元變數名稱,以指出在區域資料區的內容上執行子字串函數。
子字串函數會從指定 CL 字元變數或區域資料區的內容產生子字串。 子字串從指定的起始位置開始 (可以是變數名稱) ,並繼續達到指定的長度 (也可以是變數名稱)。 起始位置和長度都不能是 0 或負數。 如果起始位置和子字串長度的總和大於整個變數或區域資料區的長度,則會發生錯誤。 本端資料區的長度為 1024。
下列範例是關於子字串內建函數:
- If the first two positions in the character variable &NAME are IN, the program INV210 is called. The entire value of &NAME is passed to INV210 and the value of &ERRCODE is unchanged. Otherwise, the value of &ERRCODE is set to 99.
DCL &NAME *CHAR VALUE(INVOICE) DCL &ERRCODE *DEC (2 0) IF (%SST(&NAME 1 2) *EQ 'IN') + THEN(CALL INV210 &NAME) ELSE CHGVAR &ERRCODE 99
- If the first two positions of &A match the first two positions of &B, the program CUS210 is called.
DCL &A *CHAR VALUE(ABC) DCL &B *CHAR VALUE(DEF) IF (%SST(&A 1 2) *EQ %SUBSTRING(&B 1 2)) + CALL CUS210
- 位置和長度也可以是變數。 This example changes the value of &X beginning at position &Y for the length &Z to 123.
CHGVAR %SST(&X &Y &Z) '123'
- If &A is
ABCDEFG
before this CHGVAR command is run, &A isCHGVAR %SST(&A 2 3) '123'
A123EFG
。 - 在此範例中,子字串 5 的長度超出與它相比較的運算元 YES 的長度。 運算元會以空白填補,以便比較介於 YESNO 與 YESbb 之間 (其中 b 是空白)。 條件為 false。
DCL VAR(&NAME) TYPE(*CHAR) LEN(5) VALUE(YESNO) . . . IF (%SST (&NAME 1 5) *EQ YES) + THEN(CALL PROGA)
如果子字串的長度短於運算元,則會以空白填補子字串以進行比較。 例如:DCL VAR(&NAME) TYPE(*CHAR) LEN(5) VALUE(YESNO) . . . IF (%SST(&NAME 1 3 ) *EQ YESNO) THEN(CALL PROG)
此條件為 false ,因為 YESbb (其中 bb 是兩個空白) 不等於 YESNO。
- The value of the variable &A is placed into positions 1 through 10 of the local data area.
CHGVAR %SST(*LDA 1 10) &A
- If the concatenation of positions 1 through 3 of the local data area plus the constant 'XYZ' is equal to variable &A, then PROCA is called. For example, if positions 1 through 3 of the local data area contain 'ABC' and variable &A has a value of
ABCXYZ
, the test is true and PROCA is called.IF (((%SST*LDA 1 3) *CAT 'XYZ') *EQ &A) THEN(CALLPRC PROCA)
- This procedure scans the character variable &NUMBER and changes any leading zeros to blanks. 這可用於在訊息中顯示之前對欄位進行簡式編輯。
DCL &NUMBER *CHAR LEN(5) DCL &X *DEC LEN(3 0) VALUE(1) . . LOOP:IF (%SST(&NUMBER &X 1) *EQ '0') DO CHGVAR (%SST(&NUMBER &X 1)) ' ' /* Blank out */ CHGVAR &X (&X + 1) /* Increment */ IF (&X *NE 4) GOTO LOOP ENDDO
The following procedure uses the substring built-in function to find the first sentence in a 50-character field &INPUT and to place any remaining text in a field &REMAINDER. 它假設句子必須至少有 2 個字元,且沒有內嵌的句點。
PGM (&INPUT &REMAINDER) /* SEARCH */
DCL &INPUT *CHAR LEN(50)
DCL &REMAINDER *CHAR LEN(50)
DCL &X *INT /* INDEX */
DCL &L *INT /* REMAINING LENGTH */
DOFORL:
DOFOR &X 3 50
IF (%SST(&INPUT &X 1) *EQ '.') THEN(DO)
CHGVAR &L (50-&X)
CHGVAR &X (&X+1)
CHGVAR &REMAINDER %SST(&INPUT &X &L)
LEAVE
ENDDO
ENDDO
ENDPGM
程序從檢查期間的第三個位置開始。 Note that the substring function checks &INPUT from position 3 to a length of 1, which is position 3 only (length cannot be zero). If position 3 is a period, the remaining length of &INPUT is calculated. The value of &X is advanced to the beginning of the remainder, and the remaining portion of &INPUT is moved to &REMAINDER.
如果位置 3 不是句點,則程序會檢查它是否位於位置 49。 如果是的話,它會假設位置 50 是句點並傳回。 If it is not at position 49, the procedure advances &X to position 4 and repeats the process.