Many languages have powerful and useful (binary safe) string manipulation functions, while others don't, making it harder for these languages to accomplish some tasks.

This task is about creating functions to handle binary strings (strings made of arbitrary bytes, i.e. byte strings according to Wikipedia) for those languages that don't have built-in support for them.

If your language of choice does have this built-in support, show a possible alternative implementation for the functions or abilities already provided by the language.

In particular the functions you need to create are:

 String creation and destruction (when needed and if there's no garbage collection or similar mechanism)
 String assignment
 String comparison
 String cloning and copying
 Check if a string is empty
 Append a byte to a string
 Extract a substring from a string
 Replace every occurrence of a byte (or a string) in a string with another string
 Join strings


Possible contexts of use: compression algorithms (like LZW compression), L-systems (manipulation of symbols), many more.




# String creation
x = "hello world"
 
# String destruction
x = nil
 
# String assignment with a null byte
x = "a\0b"
IO.inspect x                  #=> <<97, 0, 98>>
IO.puts String.length(x)      #=> 3
 
# string comparison
if x == "hello" do
  IO.puts "equal"
else
  IO.puts "not equal"         #=> not equal
end
y = "bc"
if x < y do
  IO.puts "#{x} is lexicographically less than #{y}"  #=> a b is lexicographically less than bc
end
 
# string cloning 
xx = x
IO.puts x == xx               #=> true  （same length and content)
 
# check if empty
if x=="" do
  IO.puts "is empty"
end
if String.length(x)==0 do
  IO.puts "is empty"
end
 
# append a byte
IO.puts x <> "\07"            #=> a b 7
IO.inspect x <> "\07"         #=> <<97, 0, 98, 0, 55>>
 
# substring
IO.puts String.slice("elixir", 1..3)          #=> lix
IO.puts String.slice("elixir", 2, 3)          #=> ixi
 
# replace bytes
IO.puts String.replace("a,b,c", ",", "-")     #=> a-b-c
 
# string interpolation
a = "abc"
n = 100
IO.puts "#{a} : #{n}"         #=> abc : 100
 
# join strings
a = "hel"
b = "lo w"
c = "orld"
IO.puts a <> b <> c           #=> hello world