Binary strings: Difference between revisions
Content added Content deleted
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag) |
|||
Line 1,114: | Line 1,114: | ||
=={{header|Ecstasy}}== |
=={{header|Ecstasy}}== |
||
<syntaxhighlight lang="java"> |
<syntaxhighlight lang="java"> |
||
module BinaryStrings |
module BinaryStrings { |
||
{ |
|||
@Inject Console console; |
@Inject Console console; |
||
void run() |
void run() { |
||
{ |
|||
Byte[] mutableBytes = new Byte[]; // growable and mutable string of bytes |
Byte[] mutableBytes = new Byte[]; // growable and mutable string of bytes |
||
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) |
||
Line 1,136: | Line 1,134: | ||
); |
); |
||
mutableBytes += 0; |
mutableBytes += 0; // add a byte (using an operator) |
||
mutableBytes.add(1); |
mutableBytes.add(1); // add a byte (using the underlying method) |
||
mutableBytes.addAll(#07FF); |
mutableBytes.addAll(#07FF); // add multiple bytes (using the underlying method) |
||
console.print($|Append a byte to a string: |
console.print($|Append a byte to a string: |
||
| mutableBytes={mutableBytes} |
| mutableBytes={mutableBytes} |
||
Line 1,151: | Line 1,149: | ||
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. |
val clone = fixedLength.duplicate(); // clone the array |
||
console.print($|String cloning and copying: |
console.print($|String cloning and copying: |
||
| fixedLength={fixedLength} |
| fixedLength={fixedLength} |
||
Line 1,165: | Line 1,163: | ||
); |
); |
||
for (Int start = 0; Int index := fixedLength.indexOf(0x01, start); start = index) |
for (Int start = 0; Int index := fixedLength.indexOf(0x01, start); start = index) { |
||
{ |
|||
fixedLength[index] = 0x04; |
fixedLength[index] = 0x04; |
||
} |
|||
console.print($|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} |
||
Line 1,174: | Line 1,171: | ||
); |
); |
||
for (Int start = 0; Int index := mutableBytes.indexOf(#0107, start); start = index) |
for (Int start = 0; Int index := mutableBytes.indexOf(#0107, start); start = index) { |
||
{ |
|||
mutableBytes.replaceAll(index, #9876); |
mutableBytes.replaceAll(index, #9876); |
||
} |
|||
console.print($|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} |
||
Line 1,187: | Line 1,183: | ||
| |
| |
||
); |
); |
||
} |
|||
} |
} |
||
} |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||