在 COBOL 中连接未知长度的字符串

发布于 2024-07-05 15:53:49 字数 455 浏览 7 评论 0原文

如何在 COBOL 中将两个长度未知的字符串连接在一起? 例如:

WORKING-STORAGE.
    FIRST-NAME    PIC X(15) VALUE SPACES.
    LAST-NAME     PIC X(15) VALUE SPACES.
    FULL-NAME     PIC X(31) VALUE SPACES.

如果 FIRST-NAME = 'JOHN 'LAST-NAME = 'DOE ',我怎样才能得到:

FULL-NAME = 'JOHN DOE                       '

而不是:

FULL-NAME = 'JOHN            DOE            '

How do I concatenate together two strings, of unknown length, in COBOL? So for example:

WORKING-STORAGE.
    FIRST-NAME    PIC X(15) VALUE SPACES.
    LAST-NAME     PIC X(15) VALUE SPACES.
    FULL-NAME     PIC X(31) VALUE SPACES.

If FIRST-NAME = 'JOHN ' and LAST-NAME = 'DOE ', how can I get:

FULL-NAME = 'JOHN DOE                       '

as opposed to:

FULL-NAME = 'JOHN            DOE            '

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤芳又自赏 2024-07-12 15:53:49

我相信下面的内容会给你你想要的。

STRING
FIRST-NAME DELIMITED BY " ",
" ",
LAST-NAME DELIMITED BY SIZE
INTO FULL-NAME.

I believe the following will give you what you desire.

STRING
FIRST-NAME DELIMITED BY " ",
" ",
LAST-NAME DELIMITED BY SIZE
INTO FULL-NAME.
无声静候 2024-07-12 15:53:49

乍一看,解决方案是使用对 STRING 的引用修改将两个字符串(包括空格)连接在一起。 问题是您必须知道 FIRST-NAME 中存在多少个尾随空格,否则您将生成类似“JOHNbbbbbbbbbbbbDOE”的内容,其中 b 是空格。

没有内在的 COBOL 函数来确定字符串中尾随空格的数量,但有一个可以确定字符串中前导空格的数量。 因此,据我所知,最快的方法是反转名字,找到前导空格的数量,并使用引用修改将名字和姓氏串在一起。

您必须将这些字段添加到工作存储中:

WORK-FIELD        PIC X(15) VALUE SPACES.
TRAILING-SPACES   PIC 9(3)  VALUE ZERO.
FIELD-LENGTH      PIC 9(3)  VALUE ZERO.
  1. 反转名字
    • 将功能反向(名字)移至工作字段。
    • WORK-FIELD 现在包含前导空格,而不是尾随空格。
  2. 查找 FIRST-NAME 中尾随空格的数量
    • 检查工作场计数尾随空格是否有前导空格。
    • TRAILING-SPACE 现在包含 FIRST-NAME 中尾随空格的数量。
  3. 查找 FIRST-NAME 字段的长度
    • 计算字段长度 = 函数长度(名字)。
  4. 将两个字符串连接在一起。
    • 字符串名字(1:字段长度 - 尾随空格)“ “ 按大小分隔的姓氏,包含在全名中。

At first glance, the solution is to use reference modification to STRING together the two strings, including the space. The problem is that you must know how many trailing spaces are present in FIRST-NAME, otherwise you'll produce something like 'JOHNbbbbbbbbbbbbDOE', where b is a space.

There's no intrinsic COBOL function to determine the number of trailing spaces in a string, but there is one to determine the number of leading spaces in a string. Therefore, the fastest way, as far as I can tell, is to reverse the first name, find the number of leading spaces, and use reference modification to string together the first and last names.

You'll have to add these fields to working storage:

WORK-FIELD        PIC X(15) VALUE SPACES.
TRAILING-SPACES   PIC 9(3)  VALUE ZERO.
FIELD-LENGTH      PIC 9(3)  VALUE ZERO.
  1. Reverse the FIRST-NAME
    • MOVE FUNCTION REVERSE (FIRST-NAME) TO WORK-FIELD.
    • WORK-FIELD now contains leading spaces, instead of trailing spaces.
  2. Find the number of trailing spaces in FIRST-NAME
    • INSPECT WORK-FIELD TALLYING TRAILING-SPACES FOR LEADING SPACES.
    • TRAILING-SPACE now contains the number of trailing spaces in FIRST-NAME.
  3. Find the length of the FIRST-NAME field
    • COMPUTE FIELD-LENGTH = FUNCTION LENGTH (FIRST-NAME).
  4. Concatenate the two strings together.
    • STRING FIRST-NAME (1:FIELD-LENGTH – TRAILING-SPACES) “ “ LAST-NAME DELIMITED BY SIZE, INTO FULL-NAME.
眼泪都笑了 2024-07-12 15:53:49

您可以尝试制作一个循环以获得实际长度。

You could try making a loop for to get the real length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文