Jump to content

[TUT] Lua Strings


Recommended Posts




Hello Guys it's me again, As the tittle says today i'm going to explain to you LUA Strings try to follow me till the end.

Let's get started , String is a sequence of characters it can be initialized with three forms which includes:


Characters between single 'quotes' : Example 'This is The First Text'
Characters between double "quotes" : Example "This is The Second Text"
Characters between double [[square brackets]] : Example [[This is The Third Text]]


An other example for the above three forms are shown below.

Text = {}   
Text[1] = 'MTA SA' -- Single quotes 
Text[2] = "The Greatest Game" -- Double quotes 
Text[3] = [[You Will Ever Play]] -- Double square brackets 
for i=1,#Text do  
--[[Result : 
The Greatest Game 
You Will Ever Play]] 



Escape sequence characters are used in string to change the normal interpretation of character. for more information check the list below:

Escape Sequence Use
\a bell
\b back space
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\" double quote
\' single quote
\[ left square bracket
\] right square bracket


Some Examples:

1) Newline

local Test1 = "Hello \nCommunity"  
--[[ Result : 
2) Single quote

local Test2 = "Hello \'Community\'"  
--[[ Result : 
Hello 'Community']] 

3) Left/right square bracket

local Test3 = "Hello \[Community\]"  
--[[ Result : 
Hello [Community] ]] 



  1. Case Manipulation:




    Make all the lower case characters upper case
    Make uppercase characters lower case.


    1. local Word = "Mta";
    2. outputChatBox(string.upper(Word)) -- Result : MTA
    3. outputChatBox(string.lower(Word)) -- Result : mta
  2. Replacing a substring:


    Returns a string by replacing occurrences of findString with replaceString.


    1. local Text = "I love playing MTA"
    2. local NewText = string.gsub(Text,"MTA","FootBall")
    3. outputChatBox(NewText) -- Result : I love playing FootBall
  • Finding


    string.find(string, pattern , index , plain)
    Returns the start index and end index of the pattern in the main string.


    local Text = "Nice to meet you" 
    outputChatBox(string.find(Text,"meet")) -- Result: 9   12 
  • Reversing:


    Reverses a string.


    local String = "MTA" 
    outputChatBox(string.reverse(String)) -- Result : ATM 
  • Formatting strings:


    string.format(s, ar1, ar2, ...)
    Returns a formatted string.


    -- Date Format 
    local date = 3 
    local month = 2 
    local year = 2020 
    outputChatBox(string.format("Date  %02d/%02d/%03d", date, month, year)) -- Result: Date 03/02/2020 
  • Character and byte:
    string.char(str) | string.byte(str)
    Returns internal numeric and character respresentations


    local Char = string.char(65,66,67) 
    outputChatBox(Char) -- Result : ABC 
    local Byte = string.byte("ABCDE") 
    outputChatBox(Byte) -- Result : 65 
  • Last function:
    Check the length of the passed string.
    string.rep(string, nunber))
    Repeating the same string many times.


    Local String = "MTA" 
    outputChatBox(string.len(String)) -- Result : 3 
    outputChatBox(string.rep(String,4)) -- Result : MTAMTAMTAMTA 


A character class is used to represent a set of characters. examples shown below.



. > Represents all characters.

%a > Represents all letters.

%c > Represents all control characters.

%d > Represents all digits.

%l > Represents all lowercase letters.

%p > Represents all punctuation characters.

%s > Represents all space characters.

%u > Represents all uppercase letters.

%w > Represents all alphanumeric characters.

%x > Represents all hexadecimal digits.

%z > Represents the character with representation 0.


If you have any questions, please feel free to ask.

Edited by Walid
  • Like 2
  • Thanks 2
Link to post
Thank you dude that's what exactly i wanted :D
Great job, also add string.count and string.explode usefull functions contributed by Mta users.

These "useful functions" are rather useless now, as for they can both be done with MTAs default split function

You're doing well , sir Walid good job .

Thx guys.

Link to post

I found a mistake in

5. Formatting string

-- Date Format

local date = 3

local month = 2

loal year = 2020

outputChatBox(string.format("Date %02d/%02d/%03d", date, month, year)) -- Result: Date 03/02/2020

Link to post
Another good tutorial by you, good job!
Good Job :]

Thx Guys hope it helps.

I found a mistake in

5. Formatting string

-- Date Format

local date = 3

local month = 2

loal year = 2020

outputChatBox(string.format("Date %02d/%02d/%03d", date, month, year)) -- Result: Date 03/02/2020

'C' fixed, thx .

Link to post
  • 2 weeks later...

A good addition to this tutorial would be to teach people how to manipulate strings using regular expressions.

For example, this can be used to find a URL. This is very useful in case you need to download XHTML files from a website and read through it in order to find a URL.

local string1 = "http://forum.mtasa.com/posting.php?mode=reply&f=148&t=88427" 
local findURL = string.match(string1, "http?://[%w-_%.%?%.%+=&]+") 
print(findURL) -- Prints "http://forum.mtasa.com/posting.php?mode=reply&f=148&t=88427" 

Link to post
  • 6 months later...
  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...