在COBOL語言字串處理語句用於對字串執行多的功能操作。以下是字串處理語句:
檢查動詞是用來計數或字串中的字元替換。字串操作可以在字母,數位或字母進行。檢查操作是從左至右的字元進行。是用於字串操作的選項如下:
清點選項用於計數位符串的字元。
語法
以下是tallying選項語法理:
INSPECT input-string TALLYING output-count FOR ALL CHARACTERS
以下是使用的引數的詳細資訊:
例子
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-CNT1 PIC 9(2) VALUE 0. 01 WS-CNT2 PIC 9(2) VALUE 0. 01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'. PROCEDURE DIVISION. INSPECT WS-STRING TALLYING WS-CNT1 FOR ALL CHARACTERS. DISPLAY "WS-CNT1 : "WS-CNT1. INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL 'A'. DISPLAY "WS-CNT2 : "WS-CNT2 STOP RUN.
JCL執行以上COBOL程式。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C //STEP1 EXEC PGM=HELLO
當編譯並執行上面的程式它會產生以下結果:
WS-CNT1 : 15 WS-CNT2 : 06
替換選項用於替換的字串的字元。
語法
以下是替換選項的語法:
INSPECT input-string REPLACING ALL char1 BY char2.
以下是使用的引數的詳細資訊:
範例
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'. PROCEDURE DIVISION. DISPLAY "OLD STRING : "WS-STRING. INSPECT WS-STRING REPLACING ALL 'A' BY 'X'. DISPLAY "NEW STRING : "WS-STRING. STOP RUN.
JCL執行以上COBOL程式。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C //STEP1 EXEC PGM=HELLO
當編譯並執行上面的程式它會產生以下結果:
OLD STRING : ABCDACDADEAAAFF NEW STRING : XBCDXCDXDEXXXFF
串動詞是用來連線字串。使用STRING語句,字元的兩個或更多個字串可以被組合以形成一個更長的字串。分隔的By子句是強制性的。
語法
以下是動詞的字串的語法:
STRING ws-string1 DELIMITED BY SPACE ws-string2 DELIMITED BY SIZE INTO ws-destination-string WITH POINTER ws-count ON OVERFLOW DISPLAY message1 NOT ON OVERFLOW DISPLAY message2 END-STRING.
以下是使用的引數的詳細資訊:
例子
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STRING PIC A(30). 01 WS-STR1 PIC A(15) VALUE 'Yiibai'. 01 WS-STR2 PIC A(7) VALUE 'Welcome'. 01 WS-STR3 PIC A(7) VALUE 'To AND'. 01 WS-COUNT PIC 99 VALUE 1. PROCEDURE DIVISION. STRING WS-STR2 DELIMITED BY SIZE WS-STR3 DELIMITED BY SPACE WS-STR1 DELIMITED BY SIZE INTO WS-STRING WITH POINTER WS-COUNT ON OVERFLOW DISPLAY 'OVERFLOW!' END-STRING. DISPLAY 'WS-STRING : 'WS-STRING. DISPLAY 'WS-COUNT : 'WS-COUNT. STOP RUN.
JCL執行以上COBOL程式。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C //STEP1 EXEC PGM=HELLO
當編譯並執行上面的程式它會產生以下結果:
WS-STRING : WelcomeToYiibai WS-COUNT : 25
Unstring動詞是用來分割一個字串中為多個子字串。分隔的By子句是強制性的。
語法
以下是Unstring動詞的語法:
UNSTRING ws-string DELIMITED BY SPACE INTO ws-str1, ws-str2 WITH POINTER ws-count ON OVERFLOW DISPLAY message NOT ON OVERFLOW DISPLAY message END-UNSTRING.
例子
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STRING PIC A(30) VALUE 'WELCOME TO TUTORIALSPOINT'. 01 WS-STR1 PIC A(7). 01 WS-STR2 PIC A(2). 01 WS-STR3 PIC A(15). 01 WS-COUNT PIC 99 VALUE 1. PROCEDURE DIVISION. UNSTRING WS-STRING DELIMITED BY SPACE INTO WS-STR1, WS-STR2, WS-STR3 END-UNSTRING. DISPLAY 'WS-STR1 : 'WS-STR1. DISPLAY 'WS-STR2 : 'WS-STR2. DISPLAY 'WS-STR3 : 'WS-STR3. STOP RUN.
JCL執行以上COBOL程式。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C //STEP1 EXEC PGM=HELLO
當編譯並執行上面的程式它會產生以下結果:
WS-STR1 : WELCOME WS-STR2 : TO WS-STR3 : TUTORIALSPOINT