Forum
Delete last character of a string in LUA?
Delete last character of a string in LUA?
6 replies
1

1
2
3
2
3
text = "consolecmd" -- our string text = text:sub(1, #text - 1) print(text) -- gives "consolecm"
1
2
3
2
3
text = "consolecmd" -- our string text = text:sub(1, -2) print(text) -- gives "consolecm"
Don't use s:len() use #s instead. Cred to @
Avo for refreshing my memory.s='String';f=#s;d=',';
print(s..d..f..d..s.sub(s,f,f)) -- Result: String,6,g
print(s..d..f..d..s.sub(s,0,f-1)) -- Result: String,6,Strin
function chr_mod(s,x,y)local x,y=x or #s,y or #s;return s.sub(s,x,y)end
print(chr_mod("String")) -- Return: g
print(chr_mod("String",1,5)) -- Return: Strin
Text="Hello world!"
print(Text:sub(0,-2)) -- Result: Hello world
print(Text:sub(2,-1)) -- Result: ello world! Wrote a function to make it easier.
edited 6×, last 08.02.14 11:43:42 pm
VADemon: I know, there two ways; we just presented both.@
KimKat: There's no need to complicate it. Quote
I strongly suggest using s:len() as it's quite effective in determining the size of a string and from there you could do further calculations.
1
f = #s
Avo: I agree with you on the #s (returns string length) I am just a bit loopy. I just came from the ER and now I'm coding, so it's not easy for me to remember all things. But you're right about #s is much better in this case.
VADemon: thanks, I used your code
1

Offline