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 mascot = "Bisons"
*> compare value of strings rather than object references
   if mascot = "Bisons" *> true
*> Substring
   set s to mascot(2:3) *> s is "iso"
   set s to mascot[2:3] *> s is "son"

*> Replacement
set s to mascot::Replace("sons" "nomial") *> s is "Binomial"

*> Split
declare names = "Frank,Becky,Stephen,Helen"
declare parts as list[string]
set parts to names::Split(',') *> .NET

*> Date to string
declare dt as type DateTime
declare s as string value dt::ToString("MMM dd, yyyy") *> Oct 12, 1973
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.