Previous Topic Next topic Print topic


Strings - in COBOL and C#

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 C#

// 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[0];              // letter is H
letter = Convert.ToChar(65);          // letter is A
letter = (char)65;                    // same thing
char[] word = school.ToCharArray();   // word holds Harding
 
 
 
// String literal
string msg = @"File is c:\temp\x.dat";
// same as
string msg = "File is c:\\temp\\x.dat";
 
// String comparison
string mascot = "Beatles";
if (mascot == "Beatles")                 // true
if (mascot.Equals("Beatles"))            // true
if (mascot.ToUpper().Equals("BEATLES"))  // true
if (mascot.CompareTo("Beatles") == 0)    // true
 
// Substring
s = mascot.Substring(1, 3))     // s is "eat"
 
// 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(",".ToCharArray());

// 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"
Previous Topic Next topic Print topic