Task: Binary strings



C#: using System;

class Program
{
    static void Main()
    {
        
        var x = "hello world";

        
        x = null;

        
        x = "ab\0";
        Console.WriteLine(x);
        Console.WriteLine(x.Length); 

        
        if (x == "hello")
            Console.WriteLine("equal");
        else
            Console.WriteLine("not equal");

        if (x.CompareTo("bc") == -1)
            Console.WriteLine("x is lexicographically less than 'bc'");

        
        var c = new char[3];
        x.CopyTo(0, c, 0, 3);
        object objecty = new string(c);
        var y = new string(c);

        Console.WriteLine(x == y);      
        Console.WriteLine(x.Equals(y)); 

        Console.WriteLine(x == objecty); 

        
        var empty = "";
        string nullString = null;
        var whitespace = "   ";
        if (nullString == null && empty == string.Empty && 
            string.IsNullOrEmpty(nullString) && string.IsNullOrEmpty(empty) &&
            string.IsNullOrWhiteSpace(nullString) && string.IsNullOrWhiteSpace(empty) &&
            string.IsNullOrWhiteSpace(whitespace))
            Console.WriteLine("Strings are null, empty or whitespace");

        
        x = "helloworld";
        x += (char)83;
        Console.WriteLine(x);

        
        var slice = x.Substring(5, 5);
        Console.WriteLine(slice);

        
        var greeting = x.Replace("worldS", "");
        Console.WriteLine(greeting);

        
        var join = greeting + " " + slice;
        Console.WriteLine(join);
    }
}



Java: module BinaryStrings
    {
    @Inject Console console;
    void run()
        {
        Byte[] mutableBytes = new Byte[];       
        Byte[] fixedLength  = new Byte[10];     
        Byte[] literal      = [0, 1, 7, 0xff];  
        console.print($|String creation and assignment:
                       |  mutableBytes={mutableBytes}
                       |  fixedLength={fixedLength}
                       |  literal={literal}
                       |
                     );

        console.print($|Check if a string is empty:
                       |  mutableBytes.empty={mutableBytes.empty}
                       |  fixedLength.empty={fixedLength.empty}
                       |  literal.empty={literal.empty}
                       |
                     );

        mutableBytes += 0;                      
        mutableBytes.add(1);                    
        mutableBytes.addAll(#07FF);             
        console.print($|Append a byte to a string:
                       |  mutableBytes={mutableBytes}
                       |
                     );

        console.print($|String comparison:
                       |  mutableBytes==literal = {mutableBytes==literal}
                       |  fixedLength==literal = {fixedLength==literal}
                       |
                     );

        fixedLength = new Byte[4](i -> literal[i]);     
        val clone = fixedLength.clone();                
        console.print($|String cloning and copying:
                       |  fixedLength={fixedLength}
                       |  clone={clone}
                       |
                     );

        console.print($|Extract a substring from a string:
                       |  mutableBytes[1..2]={mutableBytes[1..2]}
                       |  fixedLength[0..2]={fixedLength[0..2]}
                       |  literal[2..3]={literal[2..3]}
                       |
                     );

        for (Int start = 0; Int index := fixedLength.indexOf(0x01, start); start = index)
            {
            fixedLength[index] = 0x04;
            }
        console.print($|Replace every occurrence of a byte in a string with another string:
                       |  fixedLength={fixedLength}
                       |
                     );

        for (Int start = 0; Int index := mutableBytes.indexOf(#0107, start); start = index)
            {
            mutableBytes.replaceAll(index, #9876);
            }
        console.print($|Replace every occurrence of a string in a string with another string:
                       |  mutableBytes={mutableBytes}
                       |
                     );

        console.print($|Join strings:
                       |  mutableBytes+fixedLength+literal={mutableBytes+fixedLength+literal}
                       |
                     );
        }
    }