Binary strings: Difference between revisions

(Separation of Haskell String Functions)
Line 15:
Possible contexts of use: compression algorithms (like [[LZW compression]]), L-systems (manipulation of symbols), many more.
 
{{trans|Tcl}}
 
{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards AND formatted transput statements removed) - tested with release 1.8.8d.fc9.i386 - ELLA has no FORMATted transput}} -->
<lang># String creation #
STRING a,b,c,d,e,f,g,h,i,j,l,r;
a := "hello world";
print((a, new line));
 
# String destruction (for garbage collection) #
b := ();
BEGIN
STRING lb:= "ABCD";
~
END; # local variable "lb" has memory recovered at END #
 
# String assignment #
c := "a"+REPR 0+"b";
print (("string length c:", UPB c, new line));# ==> 3 #
 
# String comparison #
l := "ab"; r := "CD";
 
BOOL result;
FORMAT summary = $""""g""" is "b("","NOT ")"lexicographically "g" """g""""l$ ;
 
result := l < r OR l LT r; printf((summary, l, result, "less than", r));
result := l <= r OR l LE r # OR l ≤ r #; printf((summary, l, result, "less than or equal to", r));
result := l = r OR l EQ r; printf((summary, l, result, "equal to", r));
result := l /= r OR l NE r # OR l ≠ r #; printf((summary, l, result, "not equal to", r));
result := l >= r OR l GE r # OR l ≥ r #;printf((summary, l, result, "greater than or equal to", r));
result := l > r OR l GE r; printf((summary, l, result, "greater than", r));
 
# String cloning and copying #
e := f;
 
# Check if a string is empty #
IF g = "" THEN print(("g is empty", new line)) FI;
IF UPB g = 0 THEN print(("g is empty", new line)) FI;
 
# Append a byte to a string #
h +:= "A";
 
# Append a string to a string #
h +:= "BCD";
 
# Extract a substring from a string #
i := h[2:3];
print(("Substring 2:3 of ",h," is ",i, new line));
 
# Replace every occurrences of a byte (or a string) in a string with another string #
PROC replace = (STRING string, old, new, INT count)STRING: (
INT pos;
STRING tail := string, out;
TO count WHILE string in string(old, pos, tail) DO
out +:= tail[:pos-1]+new;
tail := tail[pos+UPB old:]
OD;
out+tail
);
 
j := replace("hello world", "world", "planet", max int);
print(("After replace string: ", j, new line));
 
INT offset = 7;
# Replace a character at an offest in the string #
j[offset] := "P";
print(("After replace 7th character: ", j, new line));
 
# Insert a string at an offest in the string #
j := j[:offset-1]+"INSERTED "+j[offset:];
print(("Insert string before 7th character: ", j, new line));
 
# Join strings #
a := "hel";
b := "lo w";
c := "orld";
d := a+b+c;
 
print(("a+b+c is ",d, new line));
 
# Pack a string into the target CPU's word #
BYTES word := bytes pack(d);
 
# Extract a CHAR from a CPU word #
print((offset ELEM word, new line))</lang>
<pre>
hello world
string length c: +3
"ab" is NOT lexicographically less than "CD"
"ab" is NOT lexicographically less than or equal to "CD"
"ab" is NOT lexicographically equal to "CD"
"ab" is lexicographically not equal to "CD"
"ab" is lexicographically greater than or equal to "CD"
"ab" is lexicographically greater than "CD"
g is empty
g is empty
Substring 2:3 of ABCD is BC
After replace string: hello planet
After replace 7th character: hello Planet
Insert string before 7th character: hello INSERTED Planet
a+b+c is hello world
w
</pre>
=={{header|C}}==
'''estrings.h'''