Strings - in COBOL and C# and VB.NET

C# COBOL VB.NET
// Escape sequences
// \r    // carriage-return
// \n    // line-feed
// \t    // tab
// \\    // backslash
// \"    // quote

// String concatenation
string co = "Micro Focus\t";
co += "Ltd";  // "Micro Focus<tab>Ltd"

// Chars
char letter = school[0];              // letter is H
letter = Convert.ToChar(65);          // letter is A
letter = (char)65;                    // same thing
char[] letters = "abc".ToCharArray();
// letters now holds new[] ('a', 'b', 'c');

// Verbatim string literal
string msg = @"File is c:\temp\x.dat";
// same as
string msg = "File is c:\\temp\\x.dat";

// String comparison
string mascot = "Bisons";
if (mascot == "Bisons")                 // true
if (mascot.Equals("Bisons"))            // true
if (mascot.ToUpper().Equals("BISONS"))  // true
if (mascot.CompareTo("Bisons") == 0)    // true
// String matching - No Like equivalent, use Regex
// Substring
s = mascot.Substring(2, 3))     // s is "son"
// Replacement
s = mascot.Replace("sons", "nomial"))     // s is "Binomial"
// Split
string names = "Frank,Becky,Ethan,Braden";
string[] parts = names.Split(",".ToCharArray()); // One name in each slot
// Date to string
DateTime dt = new DateTime(1973, 10, 12);
string s = dt.ToString("MMM dd, yyyy");     // Oct 12, 1973
// int to string
int x = 2;
string y = x.ToString();     // y is "2"
// string to int
int x = Convert.ToInt32("-5");     // x is -5
// Mutable string
System.Text.StringBuilder buffer = new System.Text.StringBuilder("two ");
buffer.Append("three ");
buffer.Insert(0, "one ");
buffer.Replace("two", "TWO");
Console.WriteLine(buffer);     // Prints "one TWO three"
*> Escape sequences
*> COBOL string literals don't have escape sequences,
*> however you can specify strings as hex literals:
*> x"0d"  *> carriage-return
*> x"0a"  *> line-feed
*> x"09"  *> tab
*> "\"    *> backslash
*> """"   *> quote

*> String concatenation
    declare co as string = "Micro Focus" & x"09".
    set co to co & "Ltd"  *> "Micro Focus<tab>Ltd"

*> Chars
    declare letter as character = co[0]   *> letter is M
    set letter to 65 as character      *> "A"

    declare letters = as character occurs any = "abc".ToCharArray()
    *> letters now holds table of character ('a', 'b', 'c')

*> COBOL does not have verbatim string literals
    declare msg as string = "File is c:\temp\x.dat"

*> String comparison
    declare town = "Newbury"

*> this compares the value of
*> strings rather than object references
    if town = "Newbury"
        display "true"
    end-if

*> Substring
    display town[1:3] *> displays "ewb"

*> Replacement
*>     set s to mascot::Replace("sons" "nomial")  *> s is "Binomial"
*>     display s
*> Split
    declare names = "Frank,Becky,Stephen,Helen".
    declare parts as list[string]
    declare p as binary-long
    create parts

*> TODO: Provide JVM/.NET CONDITIONALLY COMPILED EXAMPLE OF SPLIT/REPLACE

    declare aParts as string occurs any
    declare nextPart as string
    perform until exit
        unstring names  delimited by ","
            into nextPart
            with pointer p
            not on overflow
                exit perform
            write parts from nextPart
        end-unstring
    end-perform
' Special character constants (all also accessible from ControlChars class)
' vbCr
' vbLf
' vbTab
' vbBack
' ""
' vbFormFeed
' vbVerticalTab
' vbCrLf, vbNewLine
' vbNullString

' String concatenation (use & or +)
Dim co As String = "Micro Focus" & vbTab
co = co & "Ltd" ' "Micro Focus<tab>Ltd"

' Chars
Dim letter As Char = co.Chars(0)      ' letter is H
letter = Convert.ToChar(65)               ' letter is A
letter = Chr(65)                          ' same thing

Dim letters = "abc".ToCharArray()
' letters now holds New Char(3) {"a"C, "b"C, "c"C}

' No verbatim string literals
Dim msg As String = "File is c:\temp\x.dat"

' String comparison
Dim mascot As String = "Bisons"
If (mascot = "Bisons") Then                  ' true
If (mascot.Equals("Bisons")) Then            ' true
If (mascot.ToUpper().Equals("BISONS")) Then  ' true
If (mascot.CompareTo("Bisons") = 0) Then     ' true
' String matching with Like - Regex is more powerful
If ("John 3:16" Like "Jo[Hh]? #:*") Then   'true
' Substring
s = mascot.Substring(2, 3) ' s is "son"
' Replacement
s = mascot.Replace("sons", "nomial") ' s is "Binomial"
' Split
Dim names As String = "Frank,Becky,Ethan,Braden"
Dim parts() As String = names.Split(",".ToCharArray())  ' One name in each slot
' Date to string
Dim dt As New DateTime(1973, 10, 12)
Dim s As String = "My birthday: " & dt.ToString("MMM dd, yyyy") ' Oct 12, 1973
' Integer to String
Dim x As Integer = 2
Dim y As String = x.ToString()     ' y is "2"
' String to Integer
Dim x As Integer = Convert.ToInt32("-5")     ' x is -5
' Mutable string
Dim buffer As New System.Text.StringBuilder("two ")
buffer.Append("three ")
buffer.Insert(0, "one ")
buffer.Replace("two", "TWO")
Console.WriteLine(buffer)         ' Prints "one TWO three"   ' Prints "one TWO three"