当前位置:网站首页>String - string (Lua)
String - string (Lua)
2022-07-07 19:17:00 【Just be interesting】
character string
string yes Lua One of the eight major data types in ,Lua There is no concept of characters in , All texts are string.Lua Medium string In order to 8 A bit To store .
Lua The strings in are the same as most language strings Not variable . All changes string The operation of , All generate new string.Lua in string The data structure uses the string pool realized by hash algorithm . This approach is to ensure that memory does not leak , And the performance of string comparison is greatly improved .
Representation
string There are two ways to express
- ’ Single quotation marks Such as
's' - " Double quotes Such as
"s"
They are all ways of representing string constants , It is recommended to use one , Don't mix .
Long string
string have access to [[ ]] In this way Writing of multi president strings
Such as
local s = [[ L u a]]
Escape character
| Escape character | significance | ASCII Code value ( Decimal system ) |
|---|---|---|
| \a | Ring the bell (BEL) | 007 |
| \b | Backspace (BS) , Move the current position to the previous column | 008 |
| \f | Change the page (FF), Move the current position to the beginning of the next page | 012 |
| \n | Line break (LF) , Moves the current position to the beginning of the next line | 010 |
| \r | enter (CR) , Move the current position to the beginning of the line | 013 |
| \t | Horizontal TAB (HT) ( Skip to the next TAB Location ) | 009 |
| \v | Vertical tabulation (VT) | 011 |
| \ | Represents a backslash character ’‘’ | 092 |
| ’ | Represents a single quotation mark ( apostrophe ) character | 039 |
| " | Represents a double quote character | 034 |
| \0 | Null character (NULL) | 000 |
| \ddd | 1 To 3 Any character represented by an octal number | Three octal |
| \xhh | 1 To 2 Any character represented by hexadecimal digits | Binary hexadecimal |
Common functions ( String standard library )
..: Link two strings ( It's equivalent to... In other languages + Symbol , Connect the two strings to generate a new string )
string.upper: All strings are converted to Capital .string.lower: All strings are converted to Lowercase letters .
string.gusb: In the string Replace .
string.reverse: character string reverse
string.find: Find substring , Return the start index and end index of this substring ( You can use pattern matching )
string:rep: Return string string Of n A copy
string.char: char take Convert integer numbers to characters And connect string.byte: byte transformation Characters are integer values
string.len: Calculate string length ( You can also use #)
string.sub: Intercepting string
string.gmatch: Return to one iterator function , Every time this function is called , Returns a string in str The next one found matches pattern Substring of description . If parameters pattern Description string not found , The iteration function returns nil.( Pattern matching )
string.match: Just look for the source string str The first pairing in . Parameters init Optional , Specify the starting point of the search process , The default is 1.
On successful pairing , The function returns all capture results in the pairing expression ; If the capture flag is not set , Returns the entire pairing string . When there is no successful pairing , return nil.( Pattern matching )
Lua String pattern matching in supports regular expressions ( Incomplete support ). See the official documents for details
See the official documents for specific use
UTF - 8 code
Lua It's supporting UTF-8 Of , It also provides some library functions for processing (Lua5.3).
local s = "Lua Language "
-- We need to intercept the next two Chinese characters , The method is as follows
local s1 = s:sub(utf8.offset(s, -2) -- Language
-- get UTF-8 Code length
local len = utf8.len(s) --5
-- Traverse UTF-8 Encode the characters in the string
for p, c in utf8.codes(s) do --p Is in the string s The location of , c yes utf8 Coded character encoding ( Can convert )
print(utf8.char(c))
end
Lua Also available
utf8.charandutf8.codepointTwo processing functions, character encoding conversion to string and string conversion to number
This is related to string Ofstring.charandstring.bytesimilar
Cast
In the use of Arithmetic operator when , If the operation is a string ,Lua Will try to string Type changed to Floating point numbers .
print("2" * 2) --4.0
print("2" * "2") --4.0
Empathy , Using a string Connector .., If the operation is number,Lua Will try to number The type will be converted to character string
print("20" .. 22) --2022
print(20 .. 22) --2022
Thank you for watching
边栏推荐
- Interview vipshop internship testing post, Tiktok internship testing post [true submission]
- 编译原理 实验一:词法分析器的自动实现(Lex词法分析)
- How much does it cost to develop a small program mall?
- Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- 數據驗證框架 Apache BVal 再使用
- 反爬虫的重点:识别爬虫
- 链式二叉树的基本操作(C语言实现)
- Three forms of multimedia technology commonly used in enterprise exhibition hall design
- Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"
猜你喜欢

RISCV64
![[Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question](/img/08/5f4b4e2700606554516807c01454fd.png)
[Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question

Comparison and selection of kubernetes Devops CD Tools

面试唯品会实习测试岗、抖音实习测试岗【真实投稿】

cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)

Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution

Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布

Numpy——axis

Review of network attack and defense

Calculation of torque target value (ftorque) in servo torque control mode
随机推荐
LeetCode 497(C#)
Continuous test (CT) practical experience sharing
Version 2.0 of tapdata, the open source live data platform, has been released
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
POJ 2392 Space Elevator
Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution
前首富,沉迷种田
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
PV静态创建和动态创建
Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
Teach your sister to write the message queue hand in hand
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
6. About JWT
Classification and application of enterprise MES Manufacturing Execution System
LeetCode 890(C#)
如何给“不卖笔”的晨光估值?
L1-023 output gplt (Lua)
抢占周杰伦
Redis