How to check the end of a string?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

How to check the end of a string?

Post 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 :D
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How to check the end of a string?

Post 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
Last edited by Nixola on Fri Jun 29, 2012 8:19 am, edited 2 times in total.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: How to check the end of a string?

Post 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).
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How to check the end of a string?

Post by Nixola »

Yep, I meant that
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How to check the end of a string?

Post by onedaysnotice »

thanks :D
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to check the end of a string?

Post by Robin »

Nixola wrote:Yep, I meant that
Still sub, not gsub.
Help us help you: attach a .love.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How to check the end of a string?

Post by Nixola »

Oops, that's a typo
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How to check the end of a string?

Post 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 :(
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How to check the end of a string?

Post by Nixola »

Code: Select all

str = str:sub(1, 5)
This will keep the first 5 chars and cut away the others
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How to check the end of a string?

Post by onedaysnotice »

yay thanks :D
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 11 guests