Bean Posted February 4, 2013 Share Posted February 4, 2013 Hi, so I'm still learning how to script, and I've been messing around with different functions etc, and I have this: function stringTest(p) local text = "test:110" local found = string.find(text, ":") if (found) then local realtext = text:gsub(":", "") outputChatBox(realtext, p) end addCommandHandler("strings", stringTest) I tried using the gsub function for what I'm trying to do but it didn't really work, but basically. I'm trying to turn " test:110 " into " 110 ", so just removing the ":" and everything behind it, if that makes any sense. I'm guessing I have to use string.format, but I'm not familiar with it. Can anyone at least point me in the right direction please? Thanks. Link to comment
guix Posted February 4, 2013 Share Posted February 4, 2013 Hello, you got it close: use sub instead (and add "end" after the if statement) http://codepad.org/9cM8U1zq Link to comment
Bean Posted February 4, 2013 Author Share Posted February 4, 2013 Ah, awesome. Thank you so much! Link to comment
Bean Posted February 4, 2013 Author Share Posted February 4, 2013 Derp, another question: what would I do if I wanted to just display "test", and disregard the ":110", I tried to turn (pos+1) into (pos-1) but that didn't work. Link to comment
csiguusz Posted February 4, 2013 Share Posted February 4, 2013 Just read how string.sub works, and you will be able to do it alone: http://lua-users.org/wiki/StringLibraryTutorial local text = "test:110" local pos = text:find( ":" ) if pos then text = text:sub( 1, pos-1 ) print(text) end Link to comment
Bean Posted February 4, 2013 Author Share Posted February 4, 2013 I read through that page, and thank you very much. Link to comment
Anderl Posted February 4, 2013 Share Posted February 4, 2013 You can also use split function: local var = "test:110"; outputChatBox ( split ( var, ":" )[1] ); -- 'test' outputChatBox ( split ( var, ":" )[2] ); -- '110' Both outputChatBox and split are only available in MTA, you'll need to create the split function if you want to do this in any LUA interpreter ( like Lua Demo or Lua CMD Interpreter that comes with LuaForWindows package ). Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now