COBOL 字符串處理

2021-06-15 17:15 更新

COBOL中的字符串處理語句用于對(duì)字符串執(zhí)行多個(gè)功能操作。以下是字符串處理語句:

  • Inspect
  • String
  • Unstring

Inspect

Inspect動(dòng)詞是用來計(jì)數(shù)或替換字符串中的字符??梢詫?duì)字母數(shù)字,數(shù)字或字母值執(zhí)行字符串操作。inspect操作從左到右執(zhí)行。用于字符串操作的選項(xiàng)如下:

Tallying

Tallying選項(xiàng)用于計(jì)數(shù)字符串的字符。

語法

以下是Tallying選項(xiàng)的語法:

INSPECT input-string
TALLYING output-count FOR ALL CHARACTERS

所使用的參數(shù)是:

  • input-string:要對(duì)其字符進(jìn)行計(jì)數(shù)的字符串。
  • output-count :保存字符數(shù)的數(shù)據(jù)項(xiàng)。

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執(zhí)行上述COBOL程序。

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-CNT1 : 15
WS-CNT2 : 06

Replacing

Replacing選項(xiàng)用于替換字符串字符。

語法

以下是replacing選項(xiàng)的語法:

INSPECT input-string REPLACING ALL char1 BY char2.

所使用的參數(shù)是:

  • 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執(zhí)行上述COBOL程序。

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

OLD STRING : ABCDACDADEAAAFF
NEW STRING : XBCDXCDXDEXXXFF

String

string動(dòng)詞用于連接字符串。使用STRING語句,可以組合兩個(gè)或多個(gè)字符串以形成一個(gè)更長的字符串?!?span>Delimited By”子句是強(qiáng)制性的。

語法

以下是string動(dòng)詞的語法:

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.

以下是使用的參數(shù)的詳細(xì)信息:

  • ws-string1和ws-string2:要連接的輸入字符串
  • ws-string:輸出字符串
  • ws-count:用于計(jì)算新連接的字符串的長度
  • 分隔符指定字符串的結(jié)尾
  • 指針和溢出是可選的

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執(zhí)行上述COBOL程序:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-STRING : WelcomeToTutorialspoint       
WS-COUNT : 25

Unstring

Unstring動(dòng)詞是用于將一個(gè)字符串拆分成多個(gè)子字符串。DELIMITED BY子句是強(qiáng)制性的。

語法

以下是Unstring動(dòng)詞的語法:

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執(zhí)行上述COBOL程序:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-STR1 : WELCOME
WS-STR2 : TO
WS-STR3 : TUTORIALSPOINT 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)