MTA.Castiel Posted September 17, 2022 Share Posted September 17, 2022 Hello, im trying to output a 4 digit code from a random string but there is some error so i change > local theFoundCode = string.find(theCodeString, "%d%d%d%d") to local theFoundCode = string.find(theCodeString, "%d") and now it output "8". I'm trying to use string.find and seek "6970" inside of "abcdefg69hijklmn7opqrs0tuvw71xyz72", how to actually do this? function seekCode(theSource) for index, player in ipairs (getElementsByType("player")) do local theCodeString = "abcdefg69hijklmn7opqrs0tuvw71xyz72" if theCodeString then local theFoundCode = string.find(theCodeString, "%d") outputChatBox( "The code is: " .. theCodeString .. " - FOUND: " .. theFoundCode, theSource) else outputChatBox( "n/a", theSource) end end end setTimer(seekCode, 1500, 0) Link to comment
Moderators IIYAMA Posted September 17, 2022 Moderators Share Posted September 17, 2022 20 minutes ago, MTA.Castiel said: how to actually do this? You can for example replace all non numeric characters with empty strings. string.gsub("abcdefg69hijklmn7opqrs0tuvw71xyz72", "[^%d]", "") --[[ Returns 2 values: Result: 69707172 Items replaced (with empty string): 26 ]] 1 Link to comment
MTA.Castiel Posted September 18, 2022 Author Share Posted September 18, 2022 Well i can for example use: string.gsub("abcdefg69hijklmn7opqrs0tuvw71xyz72", "[^%d]", "") But that returns all the digits inside the string resulting in a value of "69707172". I'm only having difficulty getting the first 4 of them which in this "randomly generated string" is suppose to be "6970". Still clueless as to how it should be done @ IIYAMA Link to comment
Moderators IIYAMA Posted September 18, 2022 Moderators Share Posted September 18, 2022 7 hours ago, MTA.Castiel said: I'm only having difficulty getting the first 4 of them string.sub(string.gsub("abcdefg69hijklmn7opqrs0tuvw71xyz72", "[^%d]", ""), 1, 4) -- 6970 With string.sub you can make a selection of characters. Arguments: The string Start index: 1 End index: 4 https://www.tutorialspoint.com/string-sub-function-in-lua 1 Link to comment
MTA.Castiel Posted September 18, 2022 Author Share Posted September 18, 2022 I see now, okay i got it Thank you for the link to tutorialspoint as well. 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now