Page 1 of 2
How to check the end of a string?
Posted: Fri Jun 29, 2012 5:56 am
by onedaysnotice
like suppose I have a string:
string = "abcdef"
how could I check a specific number of end characters so that I can do something like:
IF the end of string is "ef" THEN
....
END
thanks
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 7:06 am
by Nixola
Code: Select all
str = 'abcdef' --avoid using the global "string" as it's already used and you need it
if str:sub(-2) == 'ef' then --Thanks Kadoba (and Robin)
--etc.
end
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 7:18 am
by Kadoba
Nixola wrote:Code: Select all
str = 'abcdef' --avoid using the global "string" as it's already used and you need it
if str:gsub(-1, -3) == 'ef' then
--etc.
end
I think you mean str:sub(-2).
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 7:28 am
by Nixola
Yep, I meant that
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 7:36 am
by onedaysnotice
thanks
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 8:16 am
by Robin
Nixola wrote:Yep, I meant that
Still
sub, not
gsub.
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 8:19 am
by Nixola
Oops, that's a typo
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 8:31 am
by onedaysnotice
how do I truncate any excess chars?
like if i have
str = awasdawasd
how do I truncate every char after the 5th?
i've tried this
Code: Select all
if player[i].actionString:len() > 5 then
player[i].actionString:gsub(player[i].actionString:sub(6), "")
end
among other things but none of them have worked
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 8:35 am
by Nixola
This will keep the first 5 chars and cut away the others
Re: How to check the end of a string?
Posted: Fri Jun 29, 2012 8:38 am
by onedaysnotice
yay thanks