Next: 6.3 Mathematical Functions Up: 6 Predefined Functions and Previous: 6.1 Predefined Functions

6.2 String Manipulation

This library provides generic functions for string manipulation, such as finding and extracting substrings. When indexing a string, the first character has position 1. See Section 8.3 for some examples on string manipulation in Lua.

strfind (str, substr, [init, [end]])

Receives two string arguments, and returns a number. This number indicates the first position where the second argument appears in the first argument. If the second argument is not a substring of the first one, then strfind returns nil . A third optional numerical argument specifies where to start the search. Another optional numerical argument specifies where to stop it.

strlen (s)

Receives a string and returns its length.

strsub (s, i, [j])

Returns another string, which is a substring of s , starting at i and runing until j . If j is absent, it is assumed to be equal to the length of s . Particularly, the call strsub(s,1,j) returns a prefix of s with length j , while the call strsub(s,i) returns a suffix of s , starting at i .

strlower (s)

Receives a string and returns a copy of that string with all upper case letters changed to lower case. All other characters are left unchanged.

strupper (s)

Receives a string and returns a copy of that string with all lower case letters changed to upper case. All other characters are left unchanged.

ascii (s, [i])

Returns the ascii code of the character s[i] . If i is absent, it is assumed to be 1.

format (formatstring, e1, e2, ...)

This function returns a formated version of its variable number of arguments following the description given in its first argument (which must be a string). The format string follows the same rules as the printf family of standard C functions. The only differencies are that the options/modifiers * , l , L , n , p , and h are not supported, and there is an extra option, q . This option formats a string in a form suitable to be safely read back by the Lua interpreter. The string is written between double quotes, and all double quotes, returns and backslashes in the string are correctly escaped when written.

The options c , d , E , e , f , g i , o , u , X , and x all expect a number argument, while q and s expects a string.


Next: 6.3 Mathematical Functions Up: 6 Predefined Functions and Previous: 6.1 Predefined Functions