Jump to content




Check first character of a string?


  • You cannot reply to this topic
7 replies to this topic

#1 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 28 March 2015 - 03:40 PM

So, let's say I have a variable like so:
local inp = read()

and the user would enter "/hi".

How would I check if the first character of the string is a slash, (/) and if so, not do anything?

Thanks

- Mackan

#2 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 28 March 2015 - 03:46 PM

if(string.sub(read(),1,1) == "/") then
--# don't do anything?
else
--# do something?
end

you can replace the read() with your local variable obviously ;)

Edited by InDieTasten, 28 March 2015 - 03:45 PM.


#3 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 28 March 2015 - 03:54 PM

Spoiler

Well, I have this here
function input()
  while true do  
	_, p1 = os.pullEvent("key")
	if(p1 == 28) then
	  local inp = read()
	  if(string.sub(inp, 1,5) == "/nick") then
		if(string.len(string.sub(inp, 6)) >= 1) then
		   local np = string.sub(inp, 7)
		   send(name.." is now known as "..np)
		   name = np
		elseif(string.sub(inp, 1,3) == "/me") then
		  send("* "..name.." "..string.sub(inp, 4))
		elseif(string.sub(inp, 1,1) == "/") then

		end
	  else
		send("<" .. name .. "> "..inp)
	  end
	end
  end
end

If I input /me or /nick it works, but not line 13..

Edited by Mackan90096, 28 March 2015 - 03:55 PM.


#4 ItsRodrick

  • Members
  • 72 posts
  • Location= gps.locate() or "unknown"

Posted 28 March 2015 - 04:03 PM

You can replace
string.sub(var,1,1)
to
var:sub(1,1)


#5 Goof

  • Members
  • 751 posts

Posted 28 March 2015 - 04:32 PM

View PostMackan90096, on 28 March 2015 - 03:54 PM, said:

Spoiler

Well, I have this here
function input()
  while true do  
	_, p1 = os.pullEvent("key")
	if(p1 == 28) then
	  local inp = read()
	  if(string.sub(inp, 1,5) == "/nick") then
		if(string.len(string.sub(inp, 6)) >= 1) then
		   local np = string.sub(inp, 7)
		   send(name.." is now known as "..np)
		   name = np
		elseif(string.sub(inp, 1,3) == "/me") then
		  send("* "..name.." "..string.sub(inp, 4))
		elseif(string.sub(inp, 1,1) == "/") then

		end
	  else
		send("<" .. name .. "> "..inp)
	  end
	end
  end
end

If I input /me or /nick it works, but not line 13..

What are you inputting?
it wont work if you input either /me or /nick, since LUA handles code from Top-bottom.

#6 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 28 March 2015 - 04:51 PM

To give you more context, I'm making a chat program, I want this to handle commands, I don't want it to anything if the string that's inputted by read() starts with a / unless it's a recognized command..

#7 GopherAtl

  • Members
  • 888 posts

Posted 28 March 2015 - 08:22 PM

I'd do a lua pattern match at the start, myself, with a pattern and code structure basically like this:

inp=read()
--//this checks if the line matches "/<cmd>", with optional arguments separated from the
--//command with a space. the ^ and $ just require the pattern to match the whole string,
--//from beginning to end
local cmd,args = imp:match("^/(%w+)%s*(.-)$")
if cmd then
  --//cmd will only be "true" if the pattern matched, so here we know we have a command
  if cmd=="nick" then
	 --//args is the new nickname
  elseif cmd=="me" then
	 --//args is the text to pose
  else
	 --unknown command, give error locally
  end
else
  --//didn't match the cmd pattern, just text to say, so send inp
end

Edited by GopherAtl, 28 March 2015 - 08:25 PM.


#8 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 29 March 2015 - 09:55 AM

@GopherAtl

Hooray! That worked! I am forever thankful :D





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users