Walid

[TUT] Lua Strings

Recommended Posts

Sans_titre_1.png

 

Sans_titre_7.pngSans_titre_8.pngSans_titre_9.png

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  
     outputChatBox(Text[i]) 
end  
  
--[[Result : 
MTA SA 
The Greatest Game 
You Will Ever Play]] 
 

 

Sans_titre_2.png

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"  
outputChatBox(Test1)  
--[[ Result : 
Hello 
Community]] 
2) Single quote

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

3) Left/right square bracket


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

 

Sans_titre_3.png

  1. Case Manipulation:
     

     

     
    string.upper(arg)

     


    Make all the lower case characters upper case
    string.lower(arg)
    Make uppercase characters lower case.


    Example:
     

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

    [divbox=#404040]string.gsub(mainString,findString,replaceString)


    Returns a string by replacing occurrences of findString with replaceString.


     
    Example:
     

    📋
     
    1.  
    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.


    Example:
     

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

     

    string.reverse(argument)
    Reverses a string.


    Example:

    
    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


    Example:

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


    Example:

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

Sans_titre_4.png

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 1
  • Thanks 1

Share this post


Link to post
Great job, also add string.count and string.explode useful functions contributed by Mta users.

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

Share this post


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.

Share this post


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

Share this post


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 .

Share this post


Link to post
Good tut,gj & keep the good job up
Thank you for tutorial, I have learned more about strings :)

Thanks.

Share this post


Link to post

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" 

Share this post


Link to post

It's good that you make tutorials so more people could learn Lua faster, good job Walid.

Share this post


Link to post
It's good that you make tutorials so more people could learn Lua faster, good job Walid.

thx :)

Share this post


Link to post
What a nice and good-looking tutorial, mate! Two thumbs up!

thx

Share this post


Link to post

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.