LUA’s text-manipulation methods
As you might already know, HaasScript supports LUA’s own “string module”. However, these are only accessible in Script Editor and not Visual Editor. For VE, see Built-in text-manipulation commands for more. If you are already familiar with LUA and its modules, you might not find new information here.
Here is the list of LUA’s text manipulation methods:
Method | Description |
string.upper(value) | Convert all text to upper-case. |
string.lower(value) | Convert all text to lower-case. |
string.gsub(mainString, findString, replaceWith) | Replace occurring findString with replaceWith in mainString |
string.find(mainString, findString [, startIndex, endIndex]) | Returns the start and end index in a tuple of the found text, or nil if not found |
string.reverse(value) | Returns input text reversed |
string.format(…) | Returns formatted text |
string.char(value) and string.byte(value) | Returns internal numeric and character representations of input |
string.len(value) | Returns the length of input text |
string.rep(value, N) | Returns text by repeating input text N times |
.. | String concatenation operator |
Now, let’s dive into a few examples to see exactly how these strings can be manipulated with the functions above.
Case Manipulation
Here is an example of case manipulation, where we convert the input strings into upper and lower case:
text = "HaasScript is cool"
Log( string.upper(text) ) -- output: "HAASSCRIPT IS COOL"
Log( string.lower(text) ) -- output: "haasscript is cool"
Replacing a substring
Here is an example of replacing text with other text …in text:
text = "I wrote something!"
newText = string.gsub(text, "something", "a massive script")
Log( newText ) -- output: "I wrote a massive script!"
Finding and reversing
In this example we find a substring in a text and reverse our full text:
text = "HaasScript Tutorial"
-- find indices
startIndex, endIndex = string.find(text, "Tutorial")
Log ( "start: " .. startIndex .. ", end: " .. endIndex ) -- output: "start: 12, end: 19"
-- reverse text
Log( string.reverse(text) ) -- output: "lairotuT tpircSsaaH"
Formatting text
In HaasScript, the string.format()
function is similar to Lua’s print
function. It allows you to construct strings with placeholders for inserting values. These placeholders are defined by a “%” character followed by a letter that specifies the formatting rules for the corresponding argument.
The format string consists of plain text along with these directives. Each directive indicates where and how each argument should be placed within the string. You can include additional options between the “%” and the letter to control formatting details, such as specifying the number of decimal digits for floating-point numbers.
-- basic formatting
Log( string.format("Number is %d", 1234) -- output: "Number is 1234"
-- date formatting
format = "Date: %02d/%02d/%03d"
date = 3; month = 1; year = 2009
Log( string.format(format, date, month, year) ) -- output: "3/1/2009"
-- one value in different formats
formats = {
'e', -- exponential
'g', -- trimmed decimal number
'd', -- whole number
'#####X', -- hexadecimal
'o', -- octal number
'f', -- floating-point number
's', -- text string
'%' -- percentage character
}
value = 123.431
for i = 1, Count(formats) do
Log( string.format("value \"%f\" in format: %%%s = %" .. formats[i], value, formats[i], value) )
end
-- outputs:
-- value "123.431000" in format: %e = 1.234310e+002
-- value "123.431000" in format: %g = 123.431
-- value "123.431000" in format: %d = 123
-- value "123.431000" in format: %#####X = 0X7B
-- value "123.431000" in format: %o = 173
-- value "123.431000" in format: %f = 123.431000
-- value "123.431000" in format: %s = 123.431
-- value "123.431000" in format: %% = %
Character and byte representations
In this example we take a look how bytes and characters can be represented.
-- first character
Log( string.byte("Haas") ) -- output: 72
-- third character
Log( string.byte("Haas",3) ) -- output: 97
-- first character from last
Log( string.byte("Haas",-1) ) -- output: 115
-- second character
Log( string.byte("Haas",2) ) -- output: 97
-- second character from last
Log( string.byte("Haas",-2) ) -- output: 97
-- internal Numeric ASCII Conversion
Log( string.char(64) ) -- output: "@"
Other common functions
This examples shows the concatenation of strings, finding length of a text and repeating same text multiple times.
string1 = "Haas"
string2 = "Tutorial"
concatenated = string1 .. string2
-- string concatenation
Log( "Concatenated string: " .. concatenated ) -- output: "HaasTutorial"
-- Length of text
Log( "Length of string1: " .. string.len(string1) ) -- output: 4
-- repeating text
repeatedString = string.rep(string1,3)
Log( repeatedString ) -- output: "HaasHaasHaas"