Binary strings: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (Automated syntax highlighting fixup (second round - minor fixes))
m (→‎{{header|Wren}}: Changed to Wren S/H)
(10 intermediate revisions by 4 users not shown)
Line 1,111:
# value: [1, 2, 3, -127, 2, 3].diverge()</syntaxhighlight>
</li></ol>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BinaryStrings {
@Inject Console console;
void run() {
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[] literal = [0, 1, 7, 0xff]; // a "constant" string of bytes
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; // add a byte (using an operator)
mutableBytes.add(1); // add a byte (using the underlying method)
mutableBytes.addAll(#07FF); // add multiple bytes (using the underlying method)
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]); // create/copy from literal to fixedLength
val clone = fixedLength.duplicate(); // clone the array
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}
|
);
}
}
</syntaxhighlight>
 
{{out}}
<pre>
String creation and assignment:
mutableBytes=0x
fixedLength=0x00000000000000000000
literal=0x000107FF
 
Check if a string is empty:
mutableBytes.empty=True
fixedLength.empty=False
literal.empty=False
 
Append a byte to a string:
mutableBytes=0x000107FF
 
String comparison:
mutableBytes==literal = True
fixedLength==literal = False
 
String cloning and copying:
fixedLength=0x000107FF
clone=0x000107FF
 
Extract a substring from a string:
mutableBytes[1..2]=0x0107
fixedLength[0..2]=0x000107
literal[2..3]=0x07FF
 
Replace every occurrence of a byte in a string with another string:
fixedLength=0x000407FF
 
Replace every occurrence of a string in a string with another string:
mutableBytes=0x009876FF
 
Join strings:
mutableBytes+fixedLength+literal=0x009876FF000407FF000107FF
</pre>
 
=={{header|Elixir}}==
Note: Elixir data types are immutable.
Line 1,522 ⟶ 1,636:
Sleep
</syntaxhighlight>
 
 
 
 
=={{header|FutureBasic}}==
FB offers 255-character Pascal strings, and CFStrings — Apple's Core Foundation strings which are toll-free bridged with Objective-C's NSStrings.
Pascal strings are an array of bytes with the string length stored in byte 0.
CFStrings are immutable objects that encode a Unicode-compliant text string, represented as a sequence of UTF–16 code units. All lengths, character indexes, and ranges are expressed in terms of 16-bit platform-endian values, with index values starting at 0. The private contents of the string object are accessed via a pointer to its address. CFStrings are technically unlimited in length. While basic CFString manipulation is shown below, FB's CocoaUI offers a host of sophisticated accessor functions. Immutable CFStrings have a mutable sister, CFMutableStrings.
 
'''Pascal String example:'''
<syntaxhighlight lang="futurebasic">
// Pascal Strings (limited to 255 characters)
print "----------------------"
print "Pascal String Examples"
print "----------------------"
 
// Dimension strings and iterator
Str255 s, a
short i
 
// Create string
s = "Hello, world!"
 
// Get length of string using length byte at 0 index
print @"Length of \"Hello, world!\" is "; s[0]; @" characters."
 
// String destruction
s = ""
 
// String comparison
if s == "Hello, world!" then print "Strings are equal"
 
// Copying string
a = s
 
// Check If empty
if s == "" then print "String is empty"
 
// Append a byte
s = s + chr$(65)
 
// Extract a substring
a = mid$( s, 1, 5 ) // bytes 1 -> 5
 
// Substitute string "world" with "universe"
a = "Hello, world!"
for i = 1 to len$(a)
if ( mid$( a, i, 5 ) == "world" )
a = left$( a, i -1 ) + "universe" + mid$( a, i + 5 )
end if
next
print a
 
// Join strings
s = "See " + "you " + "later."
print s
print : print
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
----------------------
Pascal String Examples
----------------------
Length of "Hello, world!" is 13 characters.
String is empty
Hello, universe!
See you later.
</pre>
 
'''CFSString example:'''
<syntaxhighlight lang="futurebasic">
print @"----------------------"
print @" CFString Examples"
print @"----------------------"
 
// Dimension strings and iterator
CFStringRef c, b
NSUInteger j
 
// Create CFString as pointer to Core Foundation object
c = @"Hello, world!"
 
// Get length of string
print @"Length of \"Hello, world!\" is "; len(c); @" characters."
 
// String destruction
c = @""
 
// String comparison
if fn StringIsEqual( c, @"Hello, world!" ) then print @"Strings are equal"
 
// Copying string
b = c
 
// Check if empty
if len(c) == 0 then print @"String is empty"
 
// Append a byte
c = fn StringWithString( @"A" )
 
// Extract a substring
b = mid( c, 1, 5 )
 
// Substitute string "world" with "universe"
b = @"Hello, world!"
for j = 0 to len(b) - 1
if ( fn StringIsEqual( mid( b, j, 6 ), @"world!" ) )
b = fn StringWithFormat( @"%@%@", left( b, j ), @"universe!" )
exit for
end if
next
print b
 
// Join strings
c = fn StringWithFormat( @"%@%@%@", @"See ", @"you ", @"later." )
print c
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
----------------------
CFString Examples
----------------------
Length of "Hello, world!" is 13 characters.
String is empty
Hello, universe!
See you later.
</pre>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
Line 3,545 ⟶ 3,791:
tt= changeStr('~~', other, ";") /*change two tildes ──► one semicolon.*/
joined= dingsta || dingsta2 /*join two strings together (concat). */
say joined c2b(joined)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
c2b: returnReturn x2b( c2x( arg(1) ) ) /*return the string as a binary string.*/</syntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]].
<br><br>
{{out}}
<pre>§§ 1111010111110101</pre>
 
=={{header|Ring}}==
The String in the Ring programming language holds and manipulates an arbitrary sequence of bytes.
Line 3,597 ⟶ 3,847:
See a + b + c
</syntaxhighlight>
=={{header|RPL}}==
Strings are a sequence of bytes.
 
String creation:
"This is a new string" <span style="color:grey">@ String is put at stack level 1</span>
No need for destruction as there is a garbage collection mechanism.
 
String assignment:
"This is a string" '<span style="color:green">MYTEXT</span>' STO <span style="color:grey">@ String is transferred from stack level 1 to MYTEXT variable</span>
String comparison: numeric operators can be used for equality or lexicographic order
"This" "That" == <span style="color:grey">@ test equality - returns 0 here</span>
"This" "That" > <span style="color:grey">@ test order - returns 1 here</span>
Inclusion can also be tested:
"This" "is" POS <span style="color:grey">@ returns position of substring - 3 here</span>
String cloning and copying: any object, including strings, can be duplicated in the stack:
"This" DUP <span style="color:grey">@ Put a copy of "This" at stack level #2</span>
Check if a string is empty:
<span style="color:green">MYTEXT</span> "" == <span style="color:grey">@ returns 1 if MYTEXT contains an empty string</span>
or
<span style="color:green">MYTEXT</span> SIZE NOT <span style="color:grey">@ returns 1 if MYTEXT contains an empty string</span>
Append a byte to a string:
"Thi" "s" +
Extract a substring from a string:
"This is a string" 2 4 SUB <span style="color:grey">@ returns substring from 2nd to 4th character</span>
Replace every occurrence of a byte (or a string) in a string with another string: we need a small program here, since the REPL function replaces only the first occurence of the searched substring
≪ → string old new <span style="color:grey">@ store arguments</span>
≪ string <span style="color:grey">@ put string to be replaced in stack</span>
'''WHILE''' DUP old POS '''REPEAT''' <span style="color:grey">@ while old is present in string</span>
LAST SWAP new REPL <span style="color:grey">@ replace old by new</span>
'''END'''
≫ ≫ '<span style="color:blue">REPLALL</span>' STO
Join strings:
"This is" " a string" +
 
=={{header|Ruby}}==
A String object holds and manipulates an arbitrary sequence of bytes. There are also the [http://www.ruby-doc.org/core/classes/Array.html#M002222 Array#pack] and [http://www.ruby-doc.org/core/classes/String.html#M000760 String#unpack] methods to convert data to binary strings.
Line 3,646 ⟶ 3,930:
c = "orld"
p d = a + b + c</syntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">' Create string
Line 4,078 ⟶ 4,363:
 
Setting a string variable to the special value ''null'' means that the variable currently has no value though it can still be assigned one later.
<syntaxhighlight lang="ecmascriptwren">// create string
var s = "abc"
 
Line 4,113 ⟶ 4,398:
t = "def"
var u = s + t // "abcdef"</syntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Binary_strings
9,476

edits