RosettaCodeData/Task/Binary-strings/Lingo/binary-strings.lingo

61 lines
1.5 KiB
Plaintext

-- String creation and destruction
foo = "Hello world!" -- created by assignment; destruction via garbage collection
-- Strings are binary safe
put numtochar(0) into char 6 of foo
put chartonum(foo.char[6])
-- 0
put str.char[7..foo.length]
-- "world!"
-- String cloning and copying
bar = foo -- copies foo contents to bar
-- String comparison
put (foo=bar) -- TRUE
put (foo<>bar) -- FALSE
-- Check if a string is empty
put (foo=EMPTY)
put (foo="")
put (foo.length=0)
-- Append a byte to a string
put "X" after foo
put chartonum(88) after foo
-- Extract a substring from a string
put foo.char[3..5]
-- Replace every occurrence of a byte (or a string) in a string with another string
----------------------------------------
-- Replace in string
-- @param {string} stringToFind
-- @param {string} stringToInsert
-- @param {string} input
-- @return {string}
----------------------------------------
on replaceAll (stringToFind, stringToInsert, input)
output = ""
findLen = stringToFind.length - 1
repeat while TRUE
currOffset = offset(stringToFind, input)
if currOffset=0 then exit repeat
put input.char[1..currOffset] after output
delete the last char of output
put stringToInsert after output
delete input.char[1..(currOffset + findLen)]
end repeat
put input after output
return output
end
put replaceAll("o", "X", foo)
-- Join strings (4x the same result)
foo = "Hello " & "world!"
foo = "Hello" & numtochar(32) & "world!"
foo = "Hello" & SPACE & "world!"
foo = "Hello" && "world!"