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:

CommandDescriptionExample
StringContainsReturns TRUE if a string is found within another stringStringContains(“Hello”, “a”) == false
StringExplode/StringSplitSplits a string into an array based on set delimiterStringSplit(“Hello World!”, ” “) == {“Hello”, “World!”}
StringFromQuerySplits a query-string into an array based on ‘&’ and ‘=’ delimitersStringFromQuery(“?q=haasscript&page=2) == {“?q”: “haasscript”, “page”: “2”}
StringIndexOfReturns the index of the first occurrence of set substring or -1 if not foundStringIndexOf(“Hello World!”, ‘o’) == 4
StringJoinConcatenates (or joins) two strings together with added delimiterStringJoin(“Hello”, “World!”, ” “) == “Hello World!”
SubStringReturns a substring based on set offset and lengthSubString(“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 )
Back to: HaasScript Fundamentals > Strings