Basic Methods for operating strings in Lua

Source: Internet
Author: User

Basic Methods for operating strings in Lua

This article describes the basic methods for operating strings in Lua. It is the basic knowledge of Lua beginners. For more information, see

A string is a character sequence and a control character. A string can be initialized in three forms, including:

Character between single quotes

Double quotation marks

[] Characters [[and]

An example of the above three forms is as follows.

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 will get the following output.

The Code is as follows:

"String 1" is Lua

String 2 is Tutorial

String 3 is "Lua Tutorial"

Use strings in escape character sequences to change the normal interpretation of characters. For example, double quotation marks ("") are printed. In the above example, we have used the "escape sequence, which is listed in the following table.

String operation

Lua supports string operation strings:

Now let's look at several examples to see the behavior of these string operation functions.

Case operations

Sample Code used to manipulate String Conversion in upper and lower case 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 will get the following output.

The Code is as follows:

LUA

Lua

Replace a substring

The following is an example code of the number of occurrences of replacing a string with another one.

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 will get the following output.

The Code is as follows:

The new string is Lua Language

Search and reverse

The following is an example code used to search for string indexes and reverse strings.

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 will get the following output.

The Code is as follows:

5 12

The new string is lairotuT auL

Format a string

In many programming scenarios, we may need to print strings in a formatted manner. You can use strings. Format the function to format the output, as shown in.

The Code is as follows:

String1 = "Lua"

String2 = "Tutorial"

Number1 = 10

Number2 = 20

-- Basic string formatting

Print (string. format ("Basic formatting % 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 will get the following output.

The Code is as follows:

Basic formatting Lua Tutorial

Date formatting 02/01/2014

0.3333 [code]

Character and byte Representation

Character and byte represents a sample code, which is used to convert strings to internal representation, and vice versa.

[Code] -- Byte conversion

-- First character

Print (string. byte ("Lua "))

-- Third character

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

-- First character from last

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 will get the following output.

The Code is as follows:

76

97

97

117

117

A

Other common functions

Common string operations, including string connection, finding a string, and repeating the same string for multiple times. These operations are described 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 will get the following output.

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.