在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