Basic method of manipulating strings in Lua

Source: Internet
Author: User
Tags lua

This article mainly introduces the basic method of manipulating strings in Lua, is the basic knowledge in Lua's introductory learning, and the friends who need it can refer to

A string is a sequence of characters, as well as a control character. Strings can be initialized in three different ways, including:

The characters between single quotes

The characters between double quotes

[] the characters between [[and]]

An example of the above three forms is shown below.

The code is as follows:

string1 = "Lua"

Print ("String 1 is", string1)

string2 = ' Tutorial '

Print ("String 2 is", string2)

String3 = [["Lua Tutorial]]

Print ("String 3 is", string3)

When we run the above program, we get the output below.

The code is as follows:

' String 1 ' is Lua

String 2 is Tutorial

String 3 is "Lua Tutorial"

The escape character sequence uses a string to change the normal interpretation of the character. For example, to print double quotes ("") in the above example, we have used the escape sequence, and its use is listed in the following table.

String manipulation

LUA supports string manipulation strings:

Now, let's dive into a few examples of the behavior of these string manipulation functions.

Case actions

The sample code used to manipulate the uppercase and lowercase characters of the string conversion is as follows.

The code is as follows:

string1 = "Lua";

Print (String.upper (string1))

Print (String.Lower (string1))

When we run the above program, we get the output below.

The code is as follows:

Lua

Lua

Replace substring

The number of occurrences that replaces a string with another example code is as follows.

The code is as follows:

String = "Lua Tutorial"

--Replacing strings

NewString = String.gsub (String, "Tutorial", "Language")

Print ("The new string is", newstring)

When we run the above program, we get the output below.

The code is as follows:

The new string is Lua Language

Find and reverse

The sample code in the index and twist string of the lookup string is as follows.

The code is as follows:

String = "Lua Tutorial"

--Replacing strings

Print (String.find (String, "Tutorial"))

reversedstring = String.reverse (String)

Print ("The new string is", reversedstring)

When we run the above program, we get the output below.

The code is as follows:

5 12

The new string is Lairotut AuL

format string

In our programming many times, we may need to print the string in a formatted way. You can use a string. The Format function formats the output as shown in the following illustration.

The code is as follows:

string1 = "Lua"

string2 = "Tutorial"

Number1 = 10

Number2 = 20

--Basic string formatting

Print (String.Format ("Basic formatting%s%s", string1,string2))

--Date formatting

date = 2; month = 1; Year = 2014

Print (String.Format ("date formatting%02d/%02d/%03d", date, month, year))

--Decimal formatting

Print (String.Format ("%.4f", 1/3))

When we run the above program, we get the output below.

The code is as follows:

Basic Formatting Lua Tutorial

Date Formatting 02/01/2014

0.3333[code]

Character and Byte representations

Characters and bytes represent an example code that is used to represent internal representations from a string conversion string, and vice versa.

[code]--Byte Conversion

---------character

Print (String.byte ("Lua"))

--Third character

Print (String.byte ("Lua", 3))

--character

Print (String.byte ("Lua",-1))

--Second character

Print (String.byte ("Lua", 2))

--Second character from last

Print (String.byte ("Lua",-2))

--Internal Numeric ASCII Conversion

Print (String.char (97))

When we run the above program, we get the output below.

The code is as follows:

76

97

97

117

117

A

Other common functions

Common string operations, including string concatenation, find strings, and repeat the same string multiple lengths at a time. These actions are given in the following example.

The code is as follows:

string1 = "Lua"

string2 = "Tutorial"

--String concatenations using ...

Print ("concatenated string", string1 ...) string2)

--Length of string

Print ("Length of String1 is", String.len (string1))

--Repeating strings

repeatedstring = String.rep (string1,3)

Print (repeatedstring)

When we run the above program, we get the output below.

The code is as follows:

concatenated string luatutorial

Length of string1 is 3

Lualualua

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.