Search code examples
stringsubstringcobolgnucobol

COBOL substring between two finite points


I understand that the string_variable(start:length) can be used to get a substring of a string given a starting point and substring length, however, I am finding that I often need to get a substring between a 'start' and 'end' point.

While I know I could always do this:

SUBTRACT start FROM end GIVING len
string(start:len)

It seems cumbersome to have to do so every time when I am writing programs that use this functionality often. Is there perhaps a quicker/built-in way of achieving this?


Solution

  • How about?

        move str (start-pos : end-pos - start-pos + 1) to ...
    

    You can subtract the first from the last, but you need to add 1 to get the correct length.

    STRING is a statement name, as is START, and END is reserved. LENGTH is a function name. I avoid those in anything that looks like code.