Jump to content

Question about strings.


Bean

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...