Leilin Peng Share: Lua string

  Strings or string (String) is a string of characters consisting of numbers, letters, the underscore.

  Lua language strings can be expressed using the following three ways:

  A string of characters between single quotes.

  A string of characters between the double quotes.

  [[And]] string of characters between.

  Examples of the above string of three ways as follows:

  string1 = "Lua"

  print ( "\" string 1 is \ "", string1)

  string2 = 'codercto.com'

  print ( "String 2", string2)

  string3 = [[ "Lua Guide"]]

  print ( "string 3", string3)

  The output is the above code is executed:

  "String 1 is" ​​Lua

  String 2 is codercto.com

  3 is the string "Lua tutorial"

  Escape character represents not displayed directly, such as back key, Enter key, and so on. As the string conversion double quotes may be used "\". "

  All the escape character and the corresponding meanings:

  Meaning the escape character ASCII value (decimal)

  \ A Bell (BEL) 007

  \ B backspace (BS), the current position to a front 008

  \ F form feed (FF), the current position to the beginning of the next page 012

  \ N line feed (LF), the current position to the beginning of the next line 010

  \ R carriage return (CR), the current position to the beginning of the line 013

  \ T horizontal tab (HT) (TAB jumps to the next position) 009

  \ V vertical tabulation (VT) 011

  \\ represents a backslash character '\' 092

  \ 'For a single quote (apostrophe) character 039

  \ "Represents a double quote character 034

  \ 0 null character (NULL) 000

  \ Any character ddd1 to three octal number represented by three octal

  \ Xhh1 to two hexadecimal any character represented by two hexadecimal

  String Manipulation

  Lua provides many ways to support the operation of the string:

  Method No. & uses

  1string.upper(argument):

  All string to uppercase letters.

  2string.lower(argument):

  All string to lowercase letters.

  3string.gsub(mainString,findString,replaceString,num)

  Alternatively in the string, mainString string is to be replaced, the replaced character findString, ReplaceString character to replace, replacement times NUM (can be ignored, then replace all), such as:

  > string.gsub("aaaa","a","z",3);

  zzza 3

  4string.find (str, substr, [init, [end]])

  The search for the specified content (third parameter index) in a specified target string and returns its location. There is no nil is returned.

  > string.find("Hello Lua user", "Lua", 1)

  7 9

  5string.reverse(arg)

  To reverse a string

  > string.reverse("Lua")

  auL

  6string.format(...)

  Returns a formatted string printf-like

  > string.format("the value is:%d",4)

  the value is:4

  7string.char (arg) 和 string.byte (arg [int])

  The integer to char converted into characters and is connected, byte integer value conversion character (a character can be specified, a default character first).

  > string.char(97,98,99,100)

  abcd

  > string.byte("ABCD",4)

  68

  > string.byte("ABCD")

  65

  >

  8string.len (arg)

  Calculating the string length.

  string.len("abc")

  3

  9string.rep(string, n)

  Returns n copies of the string STRING

  > string.rep("abcd",2)

  abcdabcd

  10..

  Links two strings

  > print("www.codercto".."com")

  www.coderctocom

  11string.gmatch(str, pattern)

  Return an iterator function, every time this function is called, returns the next found in the string str in line with a substring pattern described. If the parameter string pattern described is not found, iterated function returns nil.

  > for word in string.gmatch("Hello Lua user", "%a+") do print(word) end

  Hello

  take

  user

  12string.match(str, pattern, init)

  string.match () only to find the first pair in the source string str. init parameter optional, specify the starting point of the search process, the default is 1.

  Upon successful match, the function will return all matching expression captures the results; if no capture label, the entire string is returned when the pairing is not successful match, returns nil..

  > = string.match("I have 2 questions for you.", "%d+ %a+")

  2 questions

  > = string.format("%d, %q", string.match("I have 2 questions for you.", "(%d+) (%a+)"))

  2, "questions"

  String case conversion

  The following example demonstrates how to convert a string case:

  string1 = "Lua";

  print(string.upper(string1))

  print(string.lower(string1))

  The above code execution results:

  CONTACT

  take

  String search and reverse

  The following example shows how the string with a reverse lookup operation:

  string = "Lua Tutorial"

  - Find a string

  print(string.find(string,"Tutorial"))

  reversedString = string.reverse(string)

  print ( "new string", reversedString)

  The above code execution results:

  5 12

  The new string is lairotuT auL

  String formatting

  Providing Lua String.Format () function to generate a string having a specific format, the first format is a parameter of the function, then various corresponding code data for each of the formats.

  Due to the format of the string, such that long strings of greatly improving the readability. printf format much like the C language function in ().

  The following example shows how the string formatting operation:

  The format string may escape codes comprising:

  % C - receiving a digital, and converted to ASCII character code table corresponding to

  % D,% i - receiving and converting it into a digital signed integer format

  % O - receiving a digital was converted to the octal format

  % U - takes a number and converted to an unsigned integer format

  % X - receiving and converting it into a digital hexadecimal number format, lowercase

  % X - receiving a digital and converted to hexadecimal number format, uppercase letters

  % E - receiving and converting it into a digital scientific notation format, using the lower case letter e

  % E - receiving and converting it into a digital scientific notation format using uppercase E

  % F - receiving a digital format and converted to floating point

  % G (% G) - takes a number was converted to% e (% E, the corresponding% G), and% f in the shorter format

  % Q - takes a string and converts it to be safe to read format compiler Lua

  % S - and accepts a string according to a given format parameter string

  To further refine the format, the parameter may be added in% number parameter will be read in the following order:

  (1) sign: a + sign indicates the escape character followed by a digital display so that the number of positive plus negative default display symbols only.

  0 when using a placeholder, the width of the string after the specified default placeholder when no space is filled: (2) placeholder.

  (3) alignment means that: when the specified string width, the default is right-aligned, increasing - number may be left-aligned instead.

  (4) Width value

  (5) decimal places / cutting string: the increase in the width of the fractional part of the value n, followed if f (float escapes as% 6.3f) is set to retain only the decimal floating point bits n , followed if s (escape character string, such as% 5.3s) is set that only the first n-bit string.

  string1 = "Lua"

  string2 = "Tutorial"

  number1 = 10

  number2 = 20

  - Basic String Format

  print (string.format ( "Basic Format% s% s", string1, string2))

  - Date Format

  date = 2; month = 1; year = 2014

  print (string.format ( "Date Format% 02d /% 02d /% 03d", date, month, year))

  - decimal format

  print(string.format("%.4f",1/3))

  The above code execution results:

  The basic format Lua Tutorial

  Date Format 02/01/2014

  0.3333

  Other examples:

  string.format ( "% c", 83) output S

  string.format ( "% + d", 17.0) outputs +17

  string.format ( "% 05d", 17) output 00017

  string.format ( "% o", 17) the output 21

  string.format ( "% u", 3.14) Output 3

  string.format ( "% x", 13) outputs d

  string.format ( "% X", 13) the output D

  string.format ( "% e", 1000) the output 1.000000e + 03

  string.format ( "% E", 1000) the output 1.000000E + 03

  string.format ( "% 6.3f", 13) outputs 13.000

  string.format("%q", "One\nTwo") 输出"One\

  Two"

  string.format("%s", "monkey") 输出monkey

  string.format("%10s", "monkey") 输出 monkey

  string.format("%5.3s", "monkey") 输出 mon

  Character and integer conversion

  The following examples demonstrate the character and integer conversion:

  - character conversion

  - Conversion of a character

  print(string.byte("Lua"))

  - Conversion third character

  print(string.byte("Lua",3))

  - the end of the conversion of the first character

  print(string.byte("Lua",-1))

  - The second character

  print(string.byte("Lua",2))

  - the end of the second character conversion

  print(string.byte("Lua",-2))

  - Integer ASCII character code conversion

  print(string.char(97))

  The above code execution results:

  76

  97

  97

  117

  117

  a

  Other commonly used functions

  The following examples demonstrate the operation of the other string, computing the length of the string, the connection string, string replication:

  string1 = "www."

  string2 = "codercto"

  string3 = ".com"

  - using string concatenation ..

  print ( "connect string", string1..string2..string3)

  - length of the string

  print ( "string length", string.len (string2))

  - copy the string 2 times

  repeatedString = string.rep(string2,2)

  print(repeatedString)

  The above code execution results:

  The connection string www.codercto.com

  String length 6

  coderctocodercto

  Match mode

  Lua matching pattern directly by conventional string described. It is used for pattern matching functions string.find, string.gmatch, string.gsub, string.match.

  You can also use character classes in the pattern string.

  It refers to a character class matches any character entry mode within a particular set of characters. For example,% d matches any character class number. So you can use the date string pattern '% d% d /% d% d /% d% d% d% d' Search dd / mm / yyyy format:

  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 character classes Lua support of:

  Single character (except ^ $ ()% [] * + - outside.?): Pairing with the character itself

  . (Point): paired with any character

  % A: paired with any letter

  % C: paired with any control characters (e.g., \ n)

  % D: paired with any digital

  % L: paired with any lowercase

  % P: paired with any punctuation (punctuation)

  % S: paired with a blank character

  % U: paired with any capital letters

  % W: paired with any letters / numbers

  % X: paired with any hexadecimal number

  % Z: paired with any of the characters represent 0

  % X (where x is a non-alphabetic non-numeric characters): Pairing with character x is mainly used to deal with expressions have character features - matching problems, such as %%. (^ $ ()% [] * +.?) paired with the%

  [Number of character classes]: paired with any character class [] contains e.g. [% w_] and any letter / number, or underscore character (_) pair.

  [^ A plurality of character classes]: paired with any character class is not included in [], for example, [^% s] paired with any non-blank character

  When the above written in capital character class, represents a non-match any character of this character class, for example,% S represents paired with any non-blank character e.g., '% A' of non-alphabetic characters..:

  > print(string.gsub("hello, up-down!", "%A", "."))

  hello..up.down. 4

  4 is not a string portion of the digital result, he is returned gsub second result, representing the number of replacements occur.

  There in pattern matching special characters, they have a special meaning, Lua special characters as follows:

  ( ) . % + - * ? [ ^ $

  '%' As the escape character special characters, thus '%' match points; '%%' matches the character '%'. Escape character '%' can be used not only escape special characters, it may also be used for all non-alphanumeric characters.

  Entry mode can be:

  Single character class matches the category of any single character;

  With a single character class '*' matches zero or more of a character class. This entry always match the longest possible string;

  Class with a single character '+', one or more of the match such characters. This entry always match the longest possible string;

  Class with a single character '-', the class matches zero or more characters. And '*' are different, the entry is always matching string as short as possible;

  A single character class with a '?', Will match zero or a character class. Whenever possible, it will be a match;

  % N, n herein may be from 1 to 9; a is equal to the number n matching entry capture (described later) of the substring.

  % Bxy, where x and y are two distinct characters; the entry matches x y start end, and wherein x and y are balanced string. Means that, if the string is read from left to right, each of a read on the x + 1, y on a read-1, y that is the final end of the first count to the y 0. For example, entries% b () can be matched to the bracket equilibrium expression.

  % F [set], refers to the border pattern; this will be matched to an entry in a blank within a character string before the set, and the previous character position does not belong to this set. Set set of meanings as described above. Calculation of the empty string start and end points of matched characters where there as it '\ 0' the same.

  mode:

  Mode refers to a mode entry sequence. In the top mode plus symbol '^' anchor do match from the beginning of the string. In the final surface model plus symbol '$' matching process will be anchored to the end of the string. If the '^' and '$' appears in the other position, they were no special meaning, only represent themselves.

  capture:

  Mode from a mode with a small child inside the parentheses; sub-modes are referred capture. When the matching is successful, the matching substring to capture the string is saved for future use. Catch in the order of their opening parenthesis numbered. For example, for mode "(a * (.)% W (% s *))", the string matching "a * (.)% W (% s *)" stored in the first portion of a capture (so is the number 1) "."; characters are matched by the portion 2 capture, matching to "% s *" is No. 3.

  As a special case, the empty capture () to capture the current position of the string (which is a number). For example, if the pattern "() aa ()" is applied to the string "flaaap" on, will produce two capture: 3 and 5. (Editor: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/pengpeng1208/p/11113623.html