Forum
Off Topic 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 09.02.14 12:43:42 am
@ 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
1