Applications of the string Library
Basic functions
| Function | Description | Example | Result |
|---|---|---|---|
| len | String length | string.len("abcd") | 4 |
| rep | Repeat string n times | string.rep("abcd",2) | abcdabcd |
| lower | To lowercase | string.lower("AbcD") | abcd |
| upper | To uppercase | string.upper("AbcD") | ABCD |
| format | Format string | string.format("the value is:%d",4) | the value is:4 |
| sub | Substring | string.sub("abcd",2) | bcd |
| string.sub("abcd",-2) | cd | ||
| string.sub("abcd",2,-2) | bc | ||
| string.sub("abcd",2,3) | bc | ||
| find | Find substring (position) | string.find("cdcdcdcd","ab") | nil |
| string.find("cdcdcdcd","cd") | 1 2 | ||
| string.find("cdcdcdcd","cd",7) | 7 8 | ||
| match | Find substring (content) | string.match("cdcdcdcd","ab") | nil |
| string.match("cdcdcdcd","cd") | cd | ||
| gsub | Substitute in string | string.gsub("abcdabcd","a","z") | zbcdzbcd 2 |
| string.gsub("aaaa","a","z",3) | zzza 3 | ||
| byte | Char to integer | string.byte("ABCD",4) | 68 |
| char | Integers to chars | string.char(97,98,99,100) | abcd |
Basic patterns
| Class | Description | Example | Result |
|---|---|---|---|
| . | Any char | string.find("",".") | nil |
| %s | Whitespace | string.find("ab cd","%s%s") | 3 4 |
| %S | Non-whitespace | string.find("ab cd","%S%S") | 1 2 |
| %p | Punctuation | string.find("ab,.cd","%p%p") | 3 4 |
| %P | Non-punctuation | string.find("ab,.cd","%P%P") | 1 2 |
| %c | Control char | string.find("abcd\t\n","%c%c") | 5 6 |
| %C | Non-control char | string.find("\t\nabcd","%C%C") | 3 4 |
| %d | Digit | string.find("abcd12","%d%d") | 5 6 |
| %D | Non-digit | string.find("12abcd","%D%D") | 3 4 |
| %x | Hex digit | string.find("efgh","%x%x") | 1 2 |
| %X | Non-hex digit | string.find("efgh","%X%X") | 3 4 |
| %a | Letter | string.find("AB12","%a%a") | 1 2 |
| %A | Non-letter | string.find("AB12","%A%A") | 3 4 |
| %l | Lowercase | string.find("ABab","%l%l") | 3 4 |
| %L | Uppercase | string.find("ABab","%L%L") | 1 2 |
| %u | Uppercase | string.find("ABab","%u%u") | 1 2 |
| %U | Non-uppercase | string.find("ABab","%U%U") | 3 4 |
| %w | Alnum | string.find("a1()","%w%w") | 1 2 |
| %W | Non-alnum | string.find("a1()","%W%W") | 3 4 |
Escape character %
| Class | Description | Example | Result |
|---|---|---|---|
| % | Escape percent | string.find("abc%..","%%") | 4 4 |
| string.find("abc..d","%.%.") | 4 5 |
Character classes with []
| Class | Description | Example | Result |
|---|---|---|---|
| [01] | Binary digit | string.find("32123","[01]") | 3 3 |
| [AB][CD] | AC, AD, BC, BD | string.find("ABCDEF","[AB][CD]") | 2 3 |
| [[]] | A pair of [] | string.find("ABC[]D","[[]]") | 4 5 |
| [1-3] | Digit 1-3 | string.find("312","[1-3][1-3][1-3]") | 1 3 |
| [b-d] | Letter b-d | string.find("dbc","[b-d][b-d][b-d]") | 1 3 |
| [^%s] | Non-whitespace | string.find(" a ","[^%s]") | 3 3 |
| [^%d] | Non-digit | string.find("123a","[^%d]") | 4 4 |
| [^%a] | Non-letter | string.find("abc1","[^%a]") | 4 4 |
Captures with ()
| Class | Description | Example | Result |
|---|---|---|---|
| () | Capture | string.find("12ab","(%a%a)") | 3 4 ab |
| string.find("ab12","(%d%d)") | 3 4 12 |
Pattern modifiers
| Mod | Description | Example | Result |
|---|---|---|---|
| + | 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 one | string.find("aaabbb","(a?b)") | 3 4 ab |
| string.find("cccbbb","(a?b)") | 4 4 b |
Common uses of match
| Description | Example | Result |
|---|---|---|
| Match Chinese | string.match("男女abc123","([^%w%p]+)") | 男女 |
| Match letters | string.match("男女abc123","(%a+)") | abc |
| Match digits | string.match("男女abc123","(%d+)") | 123 |
| Match letters and digits | string.match("男女abc123","(%w+)") | abc123 |