Game Development Reference
In-Depth Information
"a string with \"quotes\" and \
new line"
While most of the options c , d , E , e , f , g , G , I , o , u , X , and x expect a number as an argument, s and q
expect a string.
string.gmatch ( s, pattern )
This function returns an iterator function that returns the next capture from pattern over string s
each time it is called. If the patterns specified nothing to capture, the entire match is returned.
string.gsub ( s, pattern, repl [,n] )
This function replaces the string held in pattern with the string held in repl . n determines the
number of substitutions to be made.
str="this is a red apple"
newstr=str:gsub( "red", "green" )
print(newstr)
Here's another example that explains the replacement strings using the n parameter. In this code,
notice that the first str:gsub function replaces all of the instances of "is" with " are" . The second
function replaces the first four instances of "is" with "are" , and the last time, the str:gsub replaces
the first two instances of "is" with "are" .
str="The data is one, is two, is three, is four, is five"
print(str:gsub("is", "are"))
print(str:gsub("is", "are",4))
print(str:gsub("is", "are", 2))
string.len ( s )
This function returns the length of the string s . This is similar to the # operator. The function also
includes embedded zeros; so, for example, "a\000bc\000" has a length of 5.
string.lower ( s )
This is a function that returns the string after converting all the characters in it to lowercase, while
all other characters are left unchanged. Note that the definition of uppercase is dependent on the
current locale. For instance, in the default English locale, accented characters are not converted to
lowercase.
string.match ( s, patterns [,init] )
This function looks for and returns the first match of pattern in the string s . It will return nil if none is
found. The init parameter specifies where to start the search and can be negative.
 
Search WWH ::




Custom Search