Example:
local function setdigit() -- (string s,string i,string j,"string")
...
end
print(setdigit("abcdefg", 2, 2, "1")) -- a1cdefg
Example:
local function setdigit() -- (string s,string i,string j,"string")
...
end
print(setdigit("abcdefg", 2, 2, "1")) -- a1cdefg
Could you clarify on what you are exactly asking? I don’t understand.
I want set specific digit for string
example :
I want change “abcdefg” to “a1cdefg” but I dont know how to make this ?
local digit = string.sub(string, 2, 1)
digit = tostring(1)
I believe this should work.
… Are you tryed this ? this is always making only “1”
You can use :gsub() to replace text specific parts of a string!
local Text = "This is a random string!"
Text:gsub("This", "That")
print(Text) -- That is a random string!
(Writen on phone may have some mis spellings.)
You can’t really modify a string like that- you ‘change’ it by copying it and making a new string. To change the second digit, you can take the first digit and third onwards using string sub.
strTest = "hello"
firstLetter = string.sub(strTest,1,1)
thirdLetterOnwards = string.sub(strTest,3)
print(firstLetter.."a".. thirdLetterOnwards)
but in the system I want to use, the numbers are very likely to be the same.
You can change a specific part of string with the help of string.split() and table.concat():
function SetDigit(MainString, StartNumber, EndNumber, ReplacementString)
local ResultString = string.split(MainString, "") --Returns a table with every character as a seperate string.
for i = StartNumber, EndNumber, 1 do
ResultString[i] = ReplacementString --Loops until all specified digits are switched with a replacement string.
end
return table.concat(ResultString) --Turns a table of strings into a single one with concat operations.
end
print(SetDigit("asdasdasda", 2, 2, "1"))
--Output
->a1dasdasda
Wow Its very good
Thanks I can use this