Built-in text-manipulation commands
When logging useful information of bots actions or parsing user-input, things might become easier when you have some tools to help with that.
There are a few built-in text-manipulation commands in HaasScript and those are as follows:
Command | Description | Example |
StringContains | Returns TRUE if a string is found within another string | StringContains(“Hello”, “a”) == false |
StringExplode/StringSplit | Splits a string into an array based on set delimiter | StringSplit(“Hello World!”, ” “) == {“Hello”, “World!”} |
StringFromQuery | Splits a query-string into an array based on ‘&’ and ‘=’ delimiters | StringFromQuery(“?q=haasscript&page=2) == {“?q”: “haasscript”, “page”: “2”} |
StringIndexOf | Returns the index of the first occurrence of set substring or -1 if not found | StringIndexOf(“Hello World!”, ‘o’) == 4 |
StringJoin | Concatenates (or joins) two strings together with added delimiter | StringJoin(“Hello”, “World!”, ” “) == “Hello World!” |
SubString | Returns a substring based on set offset and length | SubString(“HaasScript”, 5, 6) == “Script” |
Few examples of use-cases
Determine whether or not we are trading on a market that’s price is based on BTC:
market = PriceMarket() ; Log( market )
local split = StringSplit(market, '_')
local exchange = split[1]
local base = split[2]
local quote = split[3]
local contract = split[4] or ''
Log('We are trading '..base..' with '..quote)
if (quote == 'BTC') then
Log('Market is quoted with BTC')
else
Log('Market is not quoted with BTC')
end
Create custom functions to parse words from sentences:
function countWords(str, delim)
delim = delim or ' ' -- default delimiter
local split = StringExplode(str, delim)
return Count(split)
end
function getWord(str, index, delim)
delim = delim or ' ' -- default delimiter if not set
local split = StringExplode(str, delim)
local count = Count(split)
-- wrap the index
if index <= 0 then
index = count + index
elseif index > count then
index = count - index
end
return split[index]
end
local words = "Hello world! This is an example of HaasScript."
local count = countWords(words)
-- forwards
for i = 1, count do
Log( i .. ' : ' .. getWord(words, i) )
end
-- backwards
for i = 1, count do
Log( i .. ' : ' .. getWord(words, count - (i-1)) )
end
Join number values into a long string:
result = '1'
for i = 2, 50 do
result = StringJoin(result, i, ', ')
end
Log( result )