需要将 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 个字符串连接到 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?"
谁能帮帮我?
这是STRING 动词的一个工作示例,可以满足您的要求:
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? <
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 answer,因为它适用于大多数(如果不是全部)编译器。
至于在 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)
语法也有效。
“以空格分隔”存在问题。如果 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-工作图片 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 "
希望它会有所帮助。
扎莱克
试一试。应该是平台无关的。
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.