Skip to content

BBScript: BBScript 2: Strings

Alteras1 edited this page Jun 11, 2024 · 3 revisions

Strings

Strings are the common value type in BBScript. These are the functions that allow for string manipulation.

Due to BBScript 2's unique literal resolution, if a literal is provided without quotes and a variable does not exist with that name, the literal will resolve to a string value.

The below would log in the console "hello" as a string.

(print hello)

Due to this quirk, it is recommended that if users wish to use a string literal, that they provide quotes.


+ Add

Adds or concatenates a string. As this uses Javascript's loose type coercion, it can either add two numbers, put two strings together, or convert a number to a string and put it together or vice versa. Takes an unlimited number of parameters.

(+ varArg1 varArg2 ...)

arguments unlimited : The values to be concatenated together. : There is no limit on the number of arguments that can be provided.

Return : Operation is not done inplace, and will return a string value to the parent.

See also Math + Add


contain

Returns if a string contains a given substring.

(contain string substring)

string : Given string to search. Must resolve to a string.

substring : string to search for. Must resolve to a string.

Return : Returns true if string contains the substring, false otherwise.


count

Returns the length of the string.

(count string)

string : Given string to count the length of.

Return : Returns the length of the string.


split

Splits a string into an array of strings. If provided a separator, will split the string using all that match that separator.

(split string separator)

string : The string to be split. Must be a string or resolve to a string.

separator optional : If provided, will split the string using the provided separator value. Must resolve to a string. : Default - By default, is an empty string. This will split the string into an array of individual characters

Return : Returns an array of strings. Will not include the separator.


slice

Returns a substring of the given string, defined at given indices. Follows the same rules as Javascript string slice().

(slice string start end)

string : The string to take the substring from. Must resolve to a string.

start : The starting index from where to begin the substring. Zero-index based. Will be the index of the first character to be included in the returned substring.

end : The ending index where the substring ends. Zero-index based. Will be the index of the first character excluded from the returned substring.

Return : Returns the substring selected.


lower

Returns the given string in all lowercase.

(lower string)

string : The string to be converted to lowercase.

Return : Returns the lowercase version of the given string.


upper

Returns the given string in all uppercase

(upper string)

string : The string to be converted to uppercase.

Return : Returns the uppercase version of the given string.


trim

Trims the given string by remove whitespace at the beginning and end of the string.

(trim string)

string : the string to be trimmed.

Return : Returns a trimmed version of the string. All whitespace at the start and end of the string is removed. Whitespace in the middle of the string is untouched.


replace

Replaces all substrings matching the needle inside of the given string with a provided replacement.

(replace string needle replacement)

string : The string to perform the replacement on.

needle : A string search for in the larger string. Will be replaced by the replacement string. Must resolve to a string.

replacement : The replacement to use if the needle is found in the string.

Return : Returns a string where all instances of the needle has been replaced by the given replacement.


String Interpolation

String interpolation can be done by use ${}.

(= foo "hello world")
(print "Msg: ${foo}")

Will print "Msg: hello world".

If the variable does not exist, then string interpolation will not replace the template and will just be treated as a normal string.