June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
60
Task/Binary-strings/Lingo/binary-strings.lingo
Normal file
60
Task/Binary-strings/Lingo/binary-strings.lingo
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
-- 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!"
|
||||
Loading…
Add table
Add a link
Reference in a new issue