Skip to main content

Applications of the string Library

Basic functions

FunctionDescriptionExampleResult
lenString lengthstring.len("abcd")4
repRepeat string n timesstring.rep("abcd",2)abcdabcd
lowerTo lowercasestring.lower("AbcD")abcd
upperTo uppercasestring.upper("AbcD")ABCD
formatFormat stringstring.format("the value is:%d",4)the value is:4
subSubstringstring.sub("abcd",2)bcd
string.sub("abcd",-2)cd
string.sub("abcd",2,-2)bc
string.sub("abcd",2,3)bc
findFind substring (position)string.find("cdcdcdcd","ab")nil
string.find("cdcdcdcd","cd")1 2
string.find("cdcdcdcd","cd",7)7 8
matchFind substring (content)string.match("cdcdcdcd","ab")nil
string.match("cdcdcdcd","cd")cd
gsubSubstitute in stringstring.gsub("abcdabcd","a","z")zbcdzbcd 2
string.gsub("aaaa","a","z",3)zzza 3
byteChar to integerstring.byte("ABCD",4)68
charIntegers to charsstring.char(97,98,99,100)abcd

Basic patterns

ClassDescriptionExampleResult
.Any charstring.find("",".")nil
%sWhitespacestring.find("ab cd","%s%s")3 4
%SNon-whitespacestring.find("ab cd","%S%S")1 2
%pPunctuationstring.find("ab,.cd","%p%p")3 4
%PNon-punctuationstring.find("ab,.cd","%P%P")1 2
%cControl charstring.find("abcd\t\n","%c%c")5 6
%CNon-control charstring.find("\t\nabcd","%C%C")3 4
%dDigitstring.find("abcd12","%d%d")5 6
%DNon-digitstring.find("12abcd","%D%D")3 4
%xHex digitstring.find("efgh","%x%x")1 2
%XNon-hex digitstring.find("efgh","%X%X")3 4
%aLetterstring.find("AB12","%a%a")1 2
%ANon-letterstring.find("AB12","%A%A")3 4
%lLowercasestring.find("ABab","%l%l")3 4
%LUppercasestring.find("ABab","%L%L")1 2
%uUppercasestring.find("ABab","%u%u")1 2
%UNon-uppercasestring.find("ABab","%U%U")3 4
%wAlnumstring.find("a1()","%w%w")1 2
%WNon-alnumstring.find("a1()","%W%W")3 4

Escape character %

ClassDescriptionExampleResult
%Escape percentstring.find("abc%..","%%")4 4
string.find("abc..d","%.%.")4 5

Character classes with []

ClassDescriptionExampleResult
[01]Binary digitstring.find("32123","[01]")3 3
[AB][CD]AC, AD, BC, BDstring.find("ABCDEF","[AB][CD]")2 3
[[]]A pair of []string.find("ABC[]D","[[]]")4 5
[1-3]Digit 1-3string.find("312","[1-3][1-3][1-3]")1 3
[b-d]Letter b-dstring.find("dbc","[b-d][b-d][b-d]")1 3
[^%s]Non-whitespacestring.find(" a ","[^%s]")3 3
[^%d]Non-digitstring.find("123a","[^%d]")4 4
[^%a]Non-letterstring.find("abc1","[^%a]")4 4

Captures with ()

ClassDescriptionExampleResult
()Capturestring.find("12ab","(%a%a)")3 4 ab
string.find("ab12","(%d%d)")3 4 12

Pattern modifiers

ModDescriptionExampleResult
+One or more (greedy)string.find("aaabbb","(a+b)")1 4 aaab
string.find("cccbbb","(a+b)")nil
-Zero or more (non-greedy)string.find("zzxyyy","(xy-)")3 3 x
string.find("zzzyyy","(x-y)")4 4 y
*Zero or more (greedy)string.find("mmmnnn","(m*n)")1 4 mmmb
string.find("lllnnn","(m*n)")4 4 n
?Zero or onestring.find("aaabbb","(a?b)")3 4 ab
string.find("cccbbb","(a?b)")4 4 b

Common uses of match

DescriptionExampleResult
Match Chinesestring.match("男女abc123","([^%w%p]+)")男女
Match lettersstring.match("男女abc123","(%a+)")abc
Match digitsstring.match("男女abc123","(%d+)")123
Match letters and digitsstring.match("男女abc123","(%w+)")abc123