content size of a string

Good afternoon, please have a string type variable and it has a text of x characters, there is a function so that I can get the exact size of this content, I am joining 3 variables in a string and getting whitespace between them , example:
set variable to var1 & var2 & var3
So, what for?
text1                           text2                                      text3
and I need you to stay
text1 text2 text3
thank you
  • One way is to use the STRING statement:

       var1 delimited by space
       var2 delimited by space
       var3 delimited by space
       into variable

  • thanks, this form I already used, but not right, as it is a text contains several spaces in this text, there I lost part of the text, I am using a class in .net visual cobol for the visual sudio
  • It's not clear to me from the question how var1 etc. are defined. Is it something like:
    01 var1 string. *> var1 is a string object
    ...or more like:
    01 var pic x(32). *> var1 is a conventional COBOL alphanumeric data item.

    If the first, you could try using the Trim function, e.g.
    set variable to var1::Trim & space & var2::Trim & space & var3::Trim.
  • In either way, it should work with the Trim or TrimEnd functions. The Trim will remove all leading and trailing spaces while TrimEnd will only remove the trailing spaces.
  • You can create a user function that computes the trailing space trimmed length of a PIC x(length) data item and then use the trimmed length in reference modification such as stringvar(1: trimmedlength) to reference the trailing space trimmed string.

    Here's a possible example of such a trimmed length user function:

    identification division.
    function-id. trimlength.

    environment division.
    configuration section.

    data division.
    working-storage section.
    01 trim-length pic 9(9) binary.

    linkage section.
    01 string1 pic x any length.
    01 length1 pic 9(9) binary.

    procedure division using string1 returning length1.

    move 0 to trim-length
    inspect function reverse(string1)
    tallying trim-length for leading spaces
    * inspect string1 tallying trim-length for trailing spaces
    compute length1 = length of string1 - trim-length
    exit function

    end function trimlength.