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

<pre><code>WORKING-STORAGE.
    FIRST-NAME    PIC X(15) VALUE SPACES.
    LAST-NAME     PIC X(15) VALUE SPACES.
    FULL-NAME     PIC X(31) VALUE SPACES.
</code></pre>

<p>If <code>FIRST-NAME = 'JOHN           '</code> and <code>LAST-NAME = 'DOE            '</code>, how can I get:</p>

<pre><code>FULL-NAME = 'JOHN DOE                       '
</code></pre>

<p>as opposed to:</p>

<pre><code>FULL-NAME = 'JOHN            DOE            '
</code></pre>

[{'answer_id': 46899, 'author': 'Eric H', 'author_id': 4650, 'author_profile': 'https://Stackoverflow.com/users/4650', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n\n<p>You'll have to add these fields to working storage:</p>\n\n<pre><code>WORK-FIELD        PIC X(15) VALUE SPACES.\nTRAILING-SPACES   PIC 9(3)  VALUE ZERO.\nFIELD-LENGTH      PIC 9(3)  VALUE ZERO.\n</code></pre>\n\n<ol>\n<li>Reverse the FIRST-NAME\n\n<ul>\n<li>MOVE FUNCTION REVERSE (FIRST-NAME) TO WORK-FIELD.</li>\n<li>WORK-FIELD now contains leading spaces, instead of trailing spaces.</li>\n</ul></li>\n<li>Find the number of trailing spaces in FIRST-NAME\n\n<ul>\n<li>INSPECT WORK-FIELD TALLYING TRAILING-SPACES FOR LEADING SPACES.</li>\n<li>TRAILING-SPACE now contains the number of trailing spaces in FIRST-NAME.</li>\n</ul></li>\n<li>Find the length of the FIRST-NAME field\n\n<ul>\n<li>COMPUTE FIELD-LENGTH = FUNCTION LENGTH (FIRST-NAME).</li>\n</ul></li>\n<li>Concatenate the two strings together.\n\n<ul>\n<li>STRING FIRST-NAME (1:FIELD-LENGTH – TRAILING-SPACES) “ “ LAST-NAME DELIMITED BY SIZE, INTO FULL-NAME.</li>\n</ul></li>\n</ol>\n"}, {'answer_id': 47119, 'author': 'Thayne', 'author_id': 3749, 'author_profile': 'https://Stackoverflow.com/users/3749', 'pm_score': 4, 'selected': True, 'text': '<p>I believe the following will give you what you desire.</p>\n\n<pre><code>STRING\nFIRST-NAME DELIMITED BY " ",\n" ",\nLAST-NAME DELIMITED BY SIZE\nINTO FULL-NAME.\n</code></pre>\n'}, {'answer_id': 55763849, 'author': 'Rubén Navarro Davó', 'author_id': 5181067, 'author_profile': 'https://Stackoverflow.com/users/5181067', 'pm_score': 0, 'selected': False, 'text': '<p>You could try making a loop for to get the real length.</p>\n'}]
['https://Stackoverflow.com/questions/46863', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4650/']