User Tools

Site Tools


lgscript:string_library

String Library

All of the string functions accept a string as the first parameter. The string table is also set as the metatable for all string values. This means that these functions are also methods for strings. If a variable has a string value, then you can call a string method, such as s:upper(). Methods can be called for literal strings by enclosing the string in parenthesis: ("lowercase"):upper().

LgScript uses Code page 850 to classify characters.

Pattern Matching

Some string functions work with patterns. A pattern is a way of describing a string in a non-specific way.

any A single character not in this list matches itself.
. Any character.
() Match a sequence of characters and store the substring as a capture.
?? An optional match of one ?.
?* Any number of characters that match ?, even none.
?+ At least one character that matches ?.
?- Any number of characters that match ?, but prefers shorter matches.
[] Any one of the characters in the group.
[^] Any one character not in the group.
%a A letter.
%c A Special non-printing character.
%d Decimal digit.
%l Lower-case letter.
%p Punctuation character.
%s A space, tab, or line-break.
%u Upper-case letter.
%w A letter or digit.
%x Hexadecimal digit.
%z The character with value 0. (NULL byte)
%digit The digits 1-9 will match a previously found substring capture in this pattern.
%b?? The two characters you type are a start and stop delimiter. Matches a substring with balanced delimiters.
%? The character you type in place of ?.
^ At the start of a pattern, matches the beginning of the string.
$ At the end of a pattern, matches the end of the string.

byte

Convert characters to byte values.

Returns number ... Value of each character code.
Parameter string String with characters to convert.
Parameter number (opt.) Index of the first character. Default is 1.
Parameter number (opt.) Index of the last character. Default is the first character index.

char

Convert byte values to a string.

Returns string String with the characters.
Parameter number ... (opt.) Character codes.

dump

Get the actual value of a function.

Returns string The compiled binary bytecode.
Parameter function A function.

Functions written in native code cannot be dumped.

find

Search for a pattern in the string.

Returns number, number, ... Start and end indexes of match, plus any captured substrings.
Returns nil No match was found.
Parameter string The string to search in.
Parameter string A pattern to look for in the string.
Parameter number (opt.) Index to begin searching at.
Parameter boolean (opt.) If true, the pattern is matched as-is.

format

Create a new string using a template to convert values.

Returns string String with the values converted.
Parameter string A format template.
Parameter ... (opt.) Values that will be inserted into the template.
c number Convert the character code to a single character.
d or i number Output a decimal number with no fractional part.
u number Output a positive decimal number with no fractional part.
o number Output an number in base-8.
x or X number Output a number in base-16. The digits A-Z will be upper or lower-case depending on which form of ‘X’ you use.
f number Output a real number.
e or E number Output a real number using exponential notation. Uses an upper or lower-case ‘E’ for the exponent indicator depending on which form you specify.
g or G number Output a real number using either decimal or exponential notation, depending on which is more compact.
s string Convert the value to a string.
q string Convert the value to a string and output it enclosed in double-quote marks. Special characters in the string will be escaped with a backslash.

gmatch

Create an iterator that searches for multiple matches of a pattern.

Returns function An iterator function that returns the next match from the string.
Parameter string String to search in.
Parameter string Pattern to look for.

gsub

Change multiple substrings in a string.

Returns string, number The modified string and the number of replacements.
Parameter string The original string.
Parameter string A pattern to search for.
Parameter string or table or function Replacement pattern, lookup table, or modifier function.
Parameter number (opt.) Maximum number of replacements to make.

len

Get the length of a string.

Returns number Length.
Parameter string A string.

lower

Change all the characters in a string to lower-case.

Returns string The modified string.
Parameter string A string.

match

Test if a string matches a pattern.

Returns ... Substring captures from the pattern.
Returns nil The string does not match.
Parameter string A string.
Parameter string A pattern.
Parameter number (opt.) Index in the string where the match should begin.

The find function will search for the pattern anywhere in the string. Match only succeeds if the match occurs at the beginning of the string, or whatever the optional index is.

rep

Repeat a string.

Returns string The created string.
Parameter string A string to repeat.
Parameter number The number of copies of the string.

reverse

Write a string backwards.

Returns string The modified string.
Parameter string A string.

sub

Get a portion of a string.

Returns string The substring.
Parameter string A string.
Parameter number Index of the first character.
Parameter number Index of the last character.

upper

Change all the characters in a string to upper-case.

Returns string The modified string.
Parameter string A string.
lgscript/string_library.txt · Last modified: 2011/05/19 00:31 by telliamed