Binary strings: Difference between revisions

Content added Content deleted
(Add Ecstasy example)
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag)
Line 1,122: Line 1,122:
Byte[] fixedLength = new Byte[10]; // fixed length string of bytes (all default to 0)
Byte[] fixedLength = new Byte[10]; // fixed length string of bytes (all default to 0)
Byte[] literal = [0, 1, 7, 0xff]; // a "constant" string of bytes
Byte[] literal = [0, 1, 7, 0xff]; // a "constant" string of bytes
console.println($|String creation and assignment:
console.print($|String creation and assignment:
| mutableBytes={mutableBytes}
| mutableBytes={mutableBytes}
| fixedLength={fixedLength}
| fixedLength={fixedLength}
| literal={literal}
| literal={literal}
|
|
);
);


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


mutableBytes += 0; // add a byte (using an operator)
mutableBytes += 0; // add a byte (using an operator)
mutableBytes.add(1); // add a byte (using the underlying method)
mutableBytes.add(1); // add a byte (using the underlying method)
mutableBytes.addAll(#07FF); // add multiple bytes (using the underlying method)
mutableBytes.addAll(#07FF); // add multiple bytes (using the underlying method)
console.println($|Append a byte to a string:
console.print($|Append a byte to a string:
| mutableBytes={mutableBytes}
| mutableBytes={mutableBytes}
|
|
);
);


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


fixedLength = new Byte[4](i -> literal[i]); // create/copy from literal to fixedLength
fixedLength = new Byte[4](i -> literal[i]); // create/copy from literal to fixedLength
val clone = fixedLength.clone(); // clone the array
val clone = fixedLength.clone(); // clone the array
console.println($|String cloning and copying:
console.print($|String cloning and copying:
| fixedLength={fixedLength}
| fixedLength={fixedLength}
| clone={clone}
| clone={clone}
|
|
);
);


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


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


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


console.println($|Join strings:
console.print($|Join strings:
| mutableBytes+fixedLength+literal={mutableBytes+fixedLength+literal}
| mutableBytes+fixedLength+literal={mutableBytes+fixedLength+literal}
|
|
);
);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>


{{out}}
Output:
<pre>
<syntaxhighlight>
String creation and assignment:
String creation and assignment:
mutableBytes=0x
mutableBytes=0x
Line 1,227: Line 1,227:
Join strings:
Join strings:
mutableBytes+fixedLength+literal=0x009876FF000407FF000107FF
mutableBytes+fixedLength+literal=0x009876FF000407FF000107FF
</pre>
</syntaxhighlight>


=={{header|Elixir}}==
=={{header|Elixir}}==