Popular Post Walid Posted May 29, 2015 Popular Post Share Posted May 29, 2015 (edited) 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]] 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] ]] Case Manipulation: string.upper(arg) Make all the lower case characters upper case string.lower(arg) Make uppercase characters lower case. Example: ? local Word = "Mta"; outputChatBox(string.upper(Word)) -- Result : MTA outputChatBox(string.lower(Word)) -- Result : mta Replacing a substring: [divbox=#404040]string.gsub(mainString,findString,replaceString) Returns a string by replacing occurrences of findString with replaceString. Example: ? local Text = "I love playing MTA" local NewText = string.gsub(Text,"MTA","FootBall") 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 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 October 13, 2016 by Walid 2 3 Link to comment
Mann56 Posted May 30, 2015 Share Posted May 30, 2015 Thank you dude that's what exactly i wanted Link to comment
Mr.unpredictable. Posted May 30, 2015 Share Posted May 30, 2015 Great job, also add string.count and string.explode usefull functions contributed by Mta users. Link to comment
xXMADEXx Posted May 30, 2015 Share Posted May 30, 2015 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. Link to comment
LaCosTa Posted May 30, 2015 Share Posted May 30, 2015 You're doing well , sir Walid good job . Link to comment
Walid Posted May 30, 2015 Author Share Posted May 30, 2015 Thank you dude that's what exactly i wanted 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 comment
xeon17 Posted May 31, 2015 Share Posted May 31, 2015 Another good tutorial by you, good job! Link to comment
Anubhav Posted May 31, 2015 Share Posted May 31, 2015 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 comment
Walid Posted May 31, 2015 Author Share Posted May 31, 2015 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 comment
KariiiM Posted June 1, 2015 Share Posted June 1, 2015 Good tut,gj & keep the good job up Link to comment
Enargy, Posted June 1, 2015 Share Posted June 1, 2015 Thank you for tutorial, I have learned more about strings Link to comment
Walid Posted June 1, 2015 Author Share Posted June 1, 2015 Good tut,gj & keep the good job up Thank you for tutorial, I have learned more about strings Thanks. Link to comment
mouamle Posted June 2, 2015 Share Posted June 2, 2015 and Another good tutorial keep it up Link to comment
Walid Posted June 3, 2015 Author Share Posted June 3, 2015 and Another good tutorial keep it up thank you Link to comment
Dealman Posted June 15, 2015 Share Posted June 15, 2015 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 = "https://forum.multitheftauto.com/posting.php?mode=reply&f=148&t=88427" local findURL = string.match(string1, "http?://[%w-_%.%?%.%+=&]+") print(findURL) -- Prints "https://forum.multitheftauto.com/posting.php?mode=reply&f=148&t=88427" Link to comment
IMariukas Posted June 19, 2015 Share Posted June 19, 2015 It's good that you make tutorials so more people could learn Lua faster, good job Walid. Link to comment
Walid Posted June 21, 2015 Author Share Posted June 21, 2015 It's good that you make tutorials so more people could learn Lua faster, good job Walid. thx Link to comment
AMARANT Posted January 15, 2016 Share Posted January 15, 2016 What a nice and good-looking tutorial, mate! Two thumbs up! Link to comment
Walid Posted March 22, 2016 Author Share Posted March 22, 2016 What a nice and good-looking tutorial, mate! Two thumbs up! thx Link to comment
Recommended Posts