按字符串长度COBOL连接字符串

时间:2012-12-20 06:50:18

标签: cobol

需要将4个字符串连接到cobol中的目标变量。

像,

01 WS-S1 X(10) VALUE "HI ".
01 WS-S2 X(10) VALUE "HOW ".
01 WS-S3 X(10) VALUE "ARE ".
01 WS-S4 X(10) VALUE "YOU?".

到结果字符串

"HI HOW ARE YOU?"

有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:20)

这是STRING verb的一个工作示例,它可以满足您的需求:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. EXAMPLE.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01    WS-S1 PIC X(10) VALUE 'HI '.
   01    WS-S2 PIC X(10) VALUE 'HOW '.
   01    WS-S3 PIC X(10) VALUE 'ARE '.
   01    WS-S4 PIC X(10) VALUE 'YOU?'.
   01    WS-CONCAT PIC X(43) VALUE SPACES.
   PROCEDURE DIVISION.
   MAIN-PARAGRAPH.
        STRING WS-S1 DELIMITED BY SPACE
               ' '   DELIMITED BY SIZE
               WS-S2 DELIMITED BY SPACE
               ' '   DELIMITED BY SIZE
               WS-S3 DELIMITED BY SPACE
               ' '   DELIMITED BY SIZE
               WS-S4 DELIMITED BY SPACE
          INTO WS-CONCAT
        END-STRING
        DISPLAY '>' WS-CONCAT '<'
        GOBACK
        .

输出是:

>HI HOW ARE YOU?                            <

答案 1 :(得分:2)

OpenCOBOL具有固有的FUNCTION扩展名CONCATENATE。

DISPLAY FUNCTION CONCATENATE(
    FUNCTION TRIM(WS-S1); SPACE;
    FUNCTION TRIM(WS-S2); SPACE;
    FUNCTION TRIM(WS-S3); SPACE;
    FUNCTION TRIM(WS-S4))
END-DISPLAY

但我喜欢STRING动词DELIMITED BY回答,因为它适用于大多数(如果不是全部)编译器。

至于FUNCTION参数列表中的分号分隔符的原因,它并非严格必要,个人偏好,因为它有时可以避免潜在的问题

SPECIAL-NAMES.
DECIMAL POINT IS COMMA.

和COBOL,它是一种强大的词汇动物

DISPLAY FUNCTION CONCATENATE(WS-S1 WS-S2 WS-S3 WS-S4)
DISPLAY FUNCTION CONCATENATE(WS-S1, WS-S2, WS-S3, WS-S4)

语法也可以。

答案 2 :(得分:0)

'空间分隔'存在问题。如果ws-s1 ='how are' - 由空格分隔将只放'how'。 以下是一些例子:

01  ws-string-test.
03 y1                pic x(10) value 'y1 a'.
03 y2                pic x(10) value 'y2 b'.
03 y3                pic x(10) value 'y3 c'.

01 ws-work pic x(200)。

       move spaces   to ws-work 
       string y1 delimited by size
              y2 delimited by space
              y3 delimited by size
              into ws-work.
       ws-work = "y1 a      y2y3 c                "

       move spaces   to ws-work 
       string y1  
              y2  
              y3
              delimited by size into ws-work
        ws-work = "y1 a      y2 b           y3 c          "


       string y1  
              y2  
              y3
              delimited by spaces into ws-work. 
        ws-work = "y1y2y3   

       string  y1 y2 y3 into ws-work by csv-format. 
       ws-work = "y1 a,y2 b,y3 c      "

希望它会有所帮助。

zalek

答案 3 :(得分:0)

给它一个旋转。应该与平台无关。

   DATA DIVISION.

   WORKING-STORAGE SECTION.

   01  result-string-text           X(100).
   01  result-string-length         9(03).
   01  result-string-datalength     9(03).

   01  new-string-text              X(20).
   01  new-string-length            9(03).
   01  new-string-datalength        9(03).

   01  hold-string-text             X(100).

   01  trailing-space-count         9(03).

   PROCEDURE DIVISION.

       MOVE SPACES TO result-string-text.
       MOVE FUNCTION LENGTH(result-string-text) TO result-string-length.
       MOVE FUNCTION LENGTH(new-string-text) TO new-string-length.

       MOVE ws-s1 TO new-string-text.
       PERFORM 5500-concatenate.

       MOVE ws-s2 TO new-string-text.
       PERFORM 5500-concatenate.

       MOVE ws-s3 TO new-string-text.
       PERFORM 5500-concatenate.

       MOVE ws-s4 TO new-string-text.
       PERFORM 5500-concatenate.

   5500-concatenate.
       MOVE ZERO TO trailing-space-count
       INSPECT FUNCTION REVERSE(result-string-text) TALLYING trailing-space-count FOR LEADING ' '
       COMPUTE result-string-datalength = result-string-length - trailing-space-count

       IF (result-string-datalength > ZERO)
           MOVE ZERO TO trailing-space-count
           INSPECT FUNCTION REVERSE(new-string-text) TALLYING trailing-space-count FOR LEADING ' '
           COMPUTE new-string-datalength = new-string-length - trailing-space-count

           MOVE SPACES TO hold-string-text
           STRING
               result-string-text(1:result-string-datalength)
               ' '
               new-string-text(1:new-string-datalength)
                   DELIMITED BY SIZE
           INTO
               hold-string-text
           END-STRING

           MOVE hold-string-text to result-string-text
       ELSE
           MOVE new-string-text TO result-string-text
       END-IF.