String Handling in Managed COBOL

Managed COBOL provides a predefined managed string type and provides powerful techniques for handling strings.

In managed COBOL, you can:

The String Predefined Type

Managed COBOL provides a predefined string type corresponding to the managed code string type. You use this type name when declaring a data item, and anywhere that a class name is expected. For example:

01 obj-string string.

Constructing String Objects

String objects are not normally constructed using the "New" method. You can simply assign a value to the string reference and the compiler automatically performs the construction for you. The string value may be set up in this way either to a literal value, or to an alphanumeric, national or group data item. For example:

       set obj-string to "   Some text..."
        display obj-string


Substringing is built into managed COBOL with the syntax (offset:stringLength). For example, the following line displays two characters of the string 'a' starting at the sixth character:

display a(6:2)    

This looks simple enough, 'a' is a string not a 'pic x'. To do this with C# or Java you have to use a substring method, whereas managed COBOL it is in syntax.


Concatenation is easy using & (ampersand).

You can do string concatenation and string reference modification in the same line; not only that but it seamlessly concatenates COBOL quoted constants with string objects. For example:

       set a to a(1:5) & "-" & a(7:)
       display a

You can concatenate strings with the following using & (ampersand):

string variables a & b
substrings a & b(1:3)
quoted string constants a & "-"
non-string types 23 & " is a number"
return values from invokes a & type COBOLString::Split(b,13,7)

The following example combines substringing with concatenation. Here a COBOL quoted constant is automatically cast to a string and the string returned from the invoke is then concatenated with two COBOL quoted constants.

       set a to "Micro Focus Managed COBOL is "                  & *>Micro Focus Managed COBOL is 
           type COBOLString::Split("The cake is amazing", 13, 7) & *>amazing
           "ly good for working with strings!"                     *>ly good for working with strings!
       display a

Where the COBOLString::Split() method contains:

       set stringOut to stringIn(offset:stringLength)

Getting the String Length

To get the length of a string, use SIZE OF. For example:

       set n to size of myString

Concatenating Numbers and Strings

The following example concatenates a string variable (a COBOL quoted constant, which is automatically cast to a string) with a number returned from an invoke, which is then concatenated with another COBOL quoted constant.

           display a                           & 
              " (this string is "              & *> (this string is
               type COBOLString::CharCount(a)  & *>69
              " bytes long)"                     *> bytes long) 

Comparing Strings

To compare strings, use = and <>. For example:

           if a = b
               display "a = b"
            if a <> b           
               display "a <> b"

String comparisons are implemented as overloads to the = and <> operators, which compare character by character (rather than compare object references).

Further Examples

See also the String Handling sample, available from Start > All Programs > Micro Focus Visual COBOL > Samples, under COBOL for .NET.