Strings - in COBOL and Java

COBOL Java
*> 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
public class Strings
{

    public static void main(String[] args)
    {
        // Escape sequences: 
        // "\n"  //  line-feed
        // "\t"  //  tab
        // "\\"    //  backslash
        // "\""   //  quote
        // string concatenation
        String co = "Micro Focus\t" ;
        co = co + "Ltd" ;  // "Micro Focus<tab>Ltd
        
        char letter = co.charAt(0);
        letter = (char) 65;  // "A"
        
        // String literal
        String msg = "File is c:\\temp\\x.dat";
        
        // String comparison
        String town = "Newbury"; 
        if (town.equals("Newbury"))
        {
            System.out.println("true");
        }
        
        // Substring
        System.out.println(town.substring(1, 3)); //Displays "ewb"
        
        // Split
        String names = "Frank,Becky,Stephen,Helen";
        String[] parts = names.split(","); // split argument is a regex 

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.