Lua String Introduction

Source: Internet
Author: User
Tags character classes

1. All character indexes in the string library are,... from the past; From behind is -1,-2,...
2. All function in the string library does not manipulate the string directly, but instead returns a result

string.byte (String [, POS]): returns the integer representation of the first POS character. If A is 97.

String.char (I1,i2 ...):I1,i2 is an integral type, will i1,i2. Converted to the corresponding character and then concatenated into a string, and returned. If I1=97 returns a.

String.dump (functoin): Returns a 2-based code for a parametric function. Questions

String.find (S,pattern [, Init [, Plain]]): finds the position of pattern in S, returns the start and end position of pattern in S. Init is the location where you started the lookup. Plain unknown.

String.len (s): Returns the length of the string.

String.Lower (s): lowercase.

String.upper (s): capitalize.

String.rep (s,n): copy S n copies, and connect and return.

string.sub (S,i [, J]): takes the self string from start I to J in S. The default j is length.-I is the reciprocal.

For example:

s = "[ABC]"
String.len (s) <== return 5

String.rep ("abc", 2) <== return "ABCABC"
String.Lower ("abc") <== return "ABC"
String.upper ("abc") <== return "ABC"
String.sub (S, 2) <== return "ABC"
String.sub (S,-2) <== return "C]"
String.sub (S, 2,-2) <== return "ABC"
String.Format (FMT, ...) Returns a formatted string similar to printf

String.find (S, pattern, POS)
1th parameter: source string
2nd parameter: The pattern string to be searched
3rd parameter: A hint, start search from POS location
Find match return: match the start and end of the string, otherwise return nil

For example:
s = "[ABC]"
String.len (s) <==

String.gsub (s, pattern, reps)
1th parameter: source string
2nd parameter: The pattern string to be replaced
3rd parameter: Replace with reps
Replaces all pattern strings in s with reps, returns the result string + match number

Print (String.gsub ("Hello, World", "O", "a")) <== Hella, Warld 2

gsubYou can also use copy capture techniques
Print (String.gsub ("Hello, World", "(O)", "%1-%1")) <== Hello-o, Wo-orld 2
Print (String.gsub ("Hello Lua", "(.) (.) ","%2%1 ") <== Ehll Oula 4

function trim (s) return (String.gsub (S, "^%s* (.-)%s*$", "%1")) End <== Note the number of matches is discarded with parentheses

String.gsub (S, Pattern, func)
3rd parameter: Custom function, matching action found, and outgoing replacement value
S, n = string.gsub ("Hello World", "L +", function (s) return "XXX" end)
Print (s, n) <== Hexxxo worxxxd 2

String.gfind (s, pattern)
Returns an iterator that executes each time and returns the next matching string;
iter = String.gfind ("A=b c=d", "[^%s+]=[^%s+]")
Print (ITER ()) <== a=b
Print (ITER ()) <== C=d

Typically used for a generic for loop, the following example results in the same
For S in String.gfind ("A=b c=d", "[^%s+]=[^%s+]") do
Print (s)
Enda simple pattern string
s = "Hello World"
I, j = string.find (S, "Hello")
Print (I, j)-1 5
Print (String.sub (S, I, J))--Hello
Print (String.find (S, "World"))--7 11
I, j = string.find (S, "L")
Print (I, j)-3 3
Print (String.find (S, "lll"))--Nil

formatted pattern string
s = "Deadline is 30/05/1999, firm"
Date = "%d%d/%d%d/%d%d%d%d"
Print (String.sub (S, string.find (s, date)))--30/05/1999

The following table lists all the character classes that LUA supports:
. Any character
%s White space character

%p Punctuation Characters
%c control character

%d numbers
%x hex Digit

%z stands for 0 characters
%a Letter
%l Small Letter
%u Capital letters
%w Letters and Numbers

The uppercase form of the above character class represents the complement of the collection represented by lowercase. For example, '%A ' characters that are not alphabetic:

special characters in a pattern string
( ) . % + - * ? [ ^ $

'% 'an escape character used as a special character
'%. ' Matching point;
' percent ' matches the character '% '.
The escape character '% ' can be used not only to escape special characters, but also for all non-alphabetic characters. When you have questions about a character, escape him with an escape character for security reasons.

Create a character set with ' [] '
' [%w_] ' matches alphanumeric and underscore
' [01] ' matches binary digits
' [%[%]] ' matches a square bracket

use hyphens '-' in ' [] '
'%d ' means ' [0-9] ';
'%x ' means ' [0-9a-fa-f] '
' [0-7] ' means ' [01234567] '

Use ' ^ ' at the beginning of ' [] ' to indicate its complement:
' [^0-7] ' matches any character that is not an octal number;
' [^/n] ' matches any characters of a non-newline character.
' [^%s] ' = = '%s '

Pattern Modifiers

+ Match previous character 1 or more times
* Match previous character 0 or more times; longest match
-Match previous character 0 or more times; shortest match
? Match previous character 0 or 1 times
^ Match string start
$ match End of string

to surround with () the part to be captured
Pair = "name = Anna"
Firstidx, Lastidx, key, value = String.find (pair, "(%a+)%s*=%s* (%a+)")
Print (key, value) <== name Anna

copy Capture (%1-%9)
s = "ABC/" it/' s a cat/""
_,_,_,q = String.find (S, "([/" ']) (.-)%1 "))
Print (q) <== It's a cat if%d represents the first few captured copies.

Lua String Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.