Highlighted
Honored Contributor.
Honored Contributor.
7349 views

String handling; carriage return line feed (new line); 0d0a

Jump to solution

Hello, I have a managed WinForms app (Visual Studio 2013, Visual COBOL 2.2) with a TextBox (Multline = True and AcceptsReturn = True) and it is sized to allow entry of multiple lines of text, which may be entered using the Enter key.  I also have the same app except it is done in C#.

As you know, using the Enter key will cause a Carriage Return Line Feed (hex oa0d aka \r\n) pair of characters (non-printable) to be embedded into textBox1::Text wherever the user has entered the Enter key, and this causes any following text to be visually put at the beginning of the next visual line, etc.  You CAN'T see these embedded non-printable characters with a watch on textBox1::Text in the debugger (although you can with C#, what a bummer!).  In any event, I then take the contents of textBox1::Text to a string and pass that string to a third-party app (MS Word, in fact).  When I get the string back from Word, it has been stripped of the Line Feeds characters but the Carriage Return characters are still there.  And I put this string back to textBox1::Text.  So, then, if in C#, if I want to put these back, I just do this (this works):

textBox1.Text = textBox1.Text.Replace("\r", System.Environment.NewLine)  

( or I think textBox1.Text = textBox1.Text.Replace("\r", "\r\n") will work too )

So, you would think in Visual COBOL, I could do this as the equivalent:

set textBox1::Text = textBox1::Text::Replace("\r", type System.Environment::NewLine)

(  or set textBox1::Text = textBox1::Text::Replace("\r", "\r\n")  )

But, it does not work.

I guess it has to do with the way strings are represented / not represented in Visual COBOL vs. other Net languages.

Can you please tell me how to do this in Visual COBOL?

Thanks,

Austin

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: String handling; carriage return line feed (new line); 0d0a

Jump to solution

The Esc characters \n, \r and \r\n are specific to C# and do not mean anything to COBOL. In COBOL you can use hex constants to define these characters for example X"0D" for carriage return and X"0A" for line feed.

Here is an example that works in COBOL:

       working-storage section.
       78 CR            value X"0D".
       01 myfield  pic x(10).
       procedure division.

           declare s1 = "test" & CR
           declare s2 = s1::Replace(CR, type System.Environment::NewLine) 
           move s2 to myfield
           display myfield

View solution in original post

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: String handling; carriage return line feed (new line); 0d0a

Jump to solution

The Esc characters \n, \r and \r\n are specific to C# and do not mean anything to COBOL. In COBOL you can use hex constants to define these characters for example X"0D" for carriage return and X"0A" for line feed.

Here is an example that works in COBOL:

       working-storage section.
       78 CR            value X"0D".
       01 myfield  pic x(10).
       procedure division.

           declare s1 = "test" & CR
           declare s2 = s1::Replace(CR, type System.Environment::NewLine) 
           move s2 to myfield
           display myfield

View solution in original post

0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

RE: String handling; carriage return line feed (new line); 0d0a

Jump to solution

Great!  Thanks!

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.