String Library
Some of the commonly used string functions are given below
string.len(s) returns the length of a string string.rep(s, n) returns a string repeated n times string.lower(s) returns the string in lower case string.upper(s) converts to upper case string.sub(s,i,j) returns a portion of the string from index i to j string.byte(s, i) returns the numeric representation of the ith character of the string string.byte(s, i, j) returns the numeric representation of the ith to jth characters string.char(number) returns the character representation of the number in ASCII For a complete listing see the references in the Contents and get the Short Reference or a Reference Card ``` ### Patterns {#patterns} * Lua does not implement full regular expressions. * It implements a smaller subset. See the reference card or short reference for the pattern matching syntax ### Pattern Matching {#pattern-matching}string.find - searches for a pattern inside a given string string.find(originalString, pattern) returns the starting index and ending index of the matched portion string.match - searches for a pattern inside a given string string.match(originalString, pattern) returns the portion of a string that matches a pattern string.gsub - substitutes a replacement string for a matching string string.gsub(originalString, pattern, replacementString, numberOfSubstitutions) returns a new string with the replacements and the total number of replacements made string.gmatch - iterates over all occurrences of a pattern in a string string.gmatch(originalString, pattern) returns the iterator, can be used with the generic for loop ``` _An example using string.match_ ``` local theString = "This is the test string that will be used to work with patterns in Lua. This should be interesting" print(string.match(theString,"t[%a%s]+g")) ``` results are: the test string * The pattern was "t[%a%s]+g" * Starts with character t * has one or more letter or whitespace * ends with character g _An example using gsub_ ``` local newString = string.gsub(theString,"[tT][eshia]+[st]","zonk") print(newString) ``` results are: zonk is the zonk string zonk will be used to work with patterns in Lua. zonk should be interesting * The pattern was [tT][eshia]+[st] * The starting character is t or T * One or more letters or spaces follow * The ending character is s or t * Replace all words matching the pattern with the word "zonk" _An example using string.gmatch_ ``` for word in string.gmatch(theString,"[tT][eshia]+[st]") do print(word) end ``` results are: This test that This * The pattern was [tT][eshia]+[st] * The starting character is t or T * One or more letters or spaces follow * The ending character is s or t ### Capture {#capture} Parentheses are used for capture in patterns _An example of capture in a pattern using gmatch_ ``` theString = "This is the test string that will be used to work with patterns in Lua. This should be interesting" -- work with capture print("\n\n") for word in string.gmatch(theString,"[tT]([eshia]+)[st]") do print(word) end ``` results are: hi es ha hi * Captures only the letters between the start and end letters</pre>