codecamp

COBOL 字符串处理

COBOL中的字符串处理语句用于对字符串执行多个功能操作。以下是字符串处理语句:

  • Inspect
  • String
  • Unstring

Inspect

Inspect动词是用来计数或替换字符串中的字符。可以对字母数字,数字或字母值执行字符串操作。inspect操作从左到右执行。用于字符串操作的选项如下:

Tallying

Tallying选项用于计数字符串的字符。

语法

以下是Tallying选项的语法:

INSPECT input-string
TALLYING output-count FOR ALL CHARACTERS

所使用的参数是:

  • input-string:要对其字符进行计数的字符串。
  • output-count :保存字符数的数据项。

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

Replacing

Replacing选项用于替换字符串字符。

语法

以下是replacing选项的语法:

INSPECT input-string REPLACING ALL char1 BY char2.

所使用的参数是:

  • input-string:要将字符从char1替换为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

string动词用于连接字符串。使用STRING语句,可以组合两个或多个字符串以形成一个更长的字符串。“Delimited By”子句是强制性的。

语法

以下是string动词的语法:

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.

以下是使用的参数的详细信息:

  • ws-string1和ws-string2:要连接的输入字符串
  • ws-string:输出字符串
  • ws-count:用于计算新连接的字符串的长度
  • 分隔符指定字符串的结尾
  • 指针和溢出是可选的

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-STRING PIC A(30).
   01 WS-STR1 PIC A(15) VALUE 'Tutorialspoint'.
   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 : WelcomeToTutorialspoint       
WS-COUNT : 25

Unstring

Unstring动词是用于将一个字符串拆分成多个子字符串。DELIMITED 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 
COBOL 循环语句
COBOL 表处理
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

COBOL 资源

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }