string 在 COBOL 中连接未知长度的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
concatenating unknown-length strings in COBOL
提问by Eric H
How do I concatenate together two strings, of unknown length, in COBOL? So for example:
如何在 COBOL 中将两个长度未知的字符串连接在一起?例如:
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:
如果FIRST-NAME = 'JOHN '
和LAST-NAME = 'DOE '
,我怎样才能得到:
FULL-NAME = 'JOHN DOE '
as opposed to:
与:
FULL-NAME = 'JOHN DOE '
回答by Thayne
I believe the following will give you what you desire.
我相信下面的内容会给你你想要的。
STRING
FIRST-NAME DELIMITED BY " ",
" ",
LAST-NAME DELIMITED BY SIZE
INTO FULL-NAME.
回答by Eric H
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.
乍一看,解决办法是使用引用修改将两个字符串串在一起,包括空格。问题是您必须知道 FIRST-NAME 中存在多少尾随空格,否则您将生成类似“JOHNbbbbbbbbbbbbDOE”的内容,其中 b 是空格。
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.
没有内在的 COBOL 函数来确定字符串中尾随空格的数量,但有一个函数可以确定字符串中前导空格的数量。因此,据我所知,最快的方法是反转名字,找到前导空格的数量,并使用引用修改将名字和姓氏串在一起。
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.
- Reverse the FIRST-NAME
- MOVE FUNCTION REVERSE (FIRST-NAME) TO WORK-FIELD.
- WORK-FIELD now contains leading spaces, instead of trailing spaces.
- 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.
- Find the length of the FIRST-NAME field
- COMPUTE FIELD-LENGTH = FUNCTION LENGTH (FIRST-NAME).
- Concatenate the two strings together.
- STRING FIRST-NAME (1:FIELD-LENGTH – TRAILING-SPACES) “ “ LAST-NAME DELIMITED BY SIZE, INTO FULL-NAME.
- 反转名字
- 将功能反向(名字)移动到工作区。
- WORK-FIELD 现在包含前导空格,而不是尾随空格。
- 查找 FIRST-NAME 中尾随空格的数量
- 检查领先空间的工作场计数尾随空间。
- TRAILING-SPACE 现在包含 FIRST-NAME 中的尾随空格数。
- 查找 FIRST-NAME 字段的长度
- 计算场长 = 函数长度(名字)。
- 将两个字符串连接在一起。
- STRING FIRST-NAME (1:FIELD-LENGTH – TRAiling-Spaces) “ “ LAST-NAME 按大小分隔,进入全名。
回答by Rubén Navarro Davó
You could try making a loop for to get the real length.
您可以尝试制作一个循环以获得真实长度。