I want to know the best way to add the numbers at the end of a string by 1, if there is none, then it is concatenated and ended with 1
.
local templateName = "Example212151513532"
local reverseString = string.reverse(templateName)
if string.find(reverseString, "%d", 1) == 1 then
local matchLastNumbers = string.match(reverseString, "%d+", #reverseString)
local numbers = string.reverse( matchLastNumbers )
templateName = string.gsub( templateName, numbers, "", 1 ).. numbers + 1
else
print("No numbers found at the end")
templateName = templateName.."1"
end
print(templateName)
My goal is to be able to change the last numbers of a string to the full number it is plus 1. Thus I need "Example212151513532"
to become "Example212151513533"
Would like to know what I’m doing wrong. Error: :6: invalid argument #1 to 'reverse' (string expected, got nil)
which I know means that matchLastNumbers
is somehow nil
yet I was able to match a number at the end of the first string.
I also would like to know if this is the best way to do it or if I can fix it but obviously it’s not correct now so I’ll leave it to after being solved for a code review.