Previous Topic Next topic Print topic


Strings - in COBOL and Java

Strings in Managed COBOL

*> Escape sequences
x"0a"  *> line-feed
x"09"  *> tab
"\"    *> backslash
""     *> quote
 
*> String concatenation
01 school string value "Harding" & x"09".
   *> school is "Harding (tab) University"
   set school to school & "University" 
 
*> Chars
01 letter character.
01 word character occurs any.
    set letter to school::Chars(0)         *> letter is H
    set letter to type Convert::ToChar(65) *> letter is A
    set letter to 65 as character          *> same thing
    set word to school::ToCharArray        *>word holds Harding
 
*> String literal
01 msg string value "File is c:\temp\x.dat".
 
  
*> String comparison
01 mascot string value "Beatles".
    if mascot = "Beatles"                    *> true
    if mascot::Equals("Beatles")             *> true
    if mascot::ToUpper::"Equals"("BEATLES")  *> true
    if mascot::CompareTo("Beatles") = 0      *> true
 
*> Substring
    set s to mascot::Substring(1, 3) *> s is "eat"
 
*> Replacement
    set s to mascot::Replace("Beatl" "Monke")  *> s is "Monkees"
 
*> Split
01 names string value "John,Paul,George,Ringo".
01 parts string occurs any.
    set parts to names::Split(",")
 
*> Date to string
01 dt type DateTime value new DateTime(1973, 10, 12).
01 s string.
    set s to dt::ToString("MMM dd, yyyy")  *> Oct 12, 1973
 
*> int to string
01 x string.
01 y binary-long value 2.
    set x to type x::ToString   *> x is "2"
        
*> string to int
01 x binary-long.
    set x to type Convert::ToInt32("-5")   *> x is -5
    
*> Mutable string
01 buffer type System.Text.StringBuilder 
        value new System.Text.StringBuilder("two ").
    invoke buffer::Append("three ")
    invoke buffer::Insert(0, "one ")
    invoke buffer::Replace("two" "TWO"
    display buffer             *> Prints "one TWO three"

Strings in Java

// Escape sequences
\r    // carriage-return
\n    // line-feed
\t    // tab
\\    // backslash
\"    // quote

// String concatenation
String school = "Harding\t";
school = school + "University"; // school is "Harding (tab) University"

	 
// Chars
char letter = school.charAt(0);       // letter is H
letter = (char)65;                    // letter is A

char[] word = school.toCharArray();   // word holds Harding
	 
	 
	 
// String literal
String msg = "File is c:\\temp\\x.dat";


	
// String comparison
String mascot = "Beatles";
if (mascot == "Beatles")                            // true
if (mascot.contentEquals("Beatles"))                // true
if (mascot.toUpperCase().contentEquals("BEATLES"))  // true
if (mascot.compareTo("Beatles") == 0)               // true

	 
// Substring NB this is different behaviour to C#, startIndex endIndex
String s = mascot.substring(1, 3);     // s is "ea"
	 
// Replacement
s = mascot.replace("Beatl", "Monke"); // s is "Monkees"
	 
// Split
String names = "John,Paul,George,Ringo";
// One name in each slot
String[] parts = names.split("',");

// Date to string
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat dateformatMMDDYYYY = 
                           new java.text.SimpleDateFormat("MMddyyyy");
StringBuilder nowMMDDYYYY = 
                     new StringBuilder(dateformatMMDDYYYY.format(dt));
s = "Today is " + nowMMDDYYYY;
	 
// int to string
int x = 2;
String y = Integer.toString(x);     // y is "2"

// String to int
String minusFive = "-5";
int z = new Double(minusFive).intValue();     // x is -5
	 
// Mutable string
java.lang.StringBuilder buffer = 
	    new java.lang.StringBuilder("two ");
buffer.append("three ");
buffer.insert(0, "one ");
buffer.replace(0, 2, "TWO"); //start, end, string - different to C#
System.out.println(buffer);     // Prints "one TWO three"

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

Previous Topic Next topic Print topic