Difference between revisions of "String.explode"

From Multi Theft Auto: Wiki
m (added useful function header)
m (added alternative code)
Line 32: Line 32:
 
     end
 
     end
 
     table.insert(pieces, ensemble:sub(position))
 
     table.insert(pieces, ensemble:sub(position))
 +
   
 +
    return pieces
 +
end
 +
</syntaxhighlight>
 +
</section>
 +
<section name="Alternative code that includes Explode in the string table" class="both" show="false">
 +
'''Note: The arguments are in another order than in Explode!'''<br />
 +
(This alternative is still untested! Please test and correct it if needed.)
 +
<syntaxhighlight lang="lua">
 +
function string:explode(separator, plain)
 +
    if (#separator == 0) then return { self } end
 +
    if (#self == 0) then return {} end
 +
    if (plain == nil) then plain = true end
 +
    local position, pieces = 1, {}
 +
   
 +
    for s, e in function() return self:find(separator, position) end do
 +
        table.insert(pieces, self:sub(position, s - 1))
 +
        position = e + 1
 +
    end
 +
    table.insert(pieces, self:sub(position))
 
      
 
      
 
     return pieces
 
     return pieces

Revision as of 10:46, 14 May 2009


This function splits a string at a given separator and stores the pieces in a table. It's the complement of table.concat.

There is already an MTA function called split that splits a string at a given separator. But this function only supports single character separators and no regular expressions. For splitting at a single character you should prefer that function since it's probably faster than Explode.

Syntax

table Explode( string separator, string ensemble, [ bool plain ] )

Required Arguments

  • separator: The string, which the ensemble shall be split at.
  • ensemble: The string to split.

Optional Arguments

  • plain: When set to true the separator is taken as normal string. This is also the default. When set to false separator is taken as regular expression.

Returns

Returns a table containing the pieces of the split ensemble.

Code

Click to collapse [-]
Server- and/or clientside Script
function Explode(separator, ensemble, plain)
    if (#separator == 0) then return { ensemble } end
    if (#ensemble == 0) then return {} end
    if (plain == nil) then plain = true end
    local position, pieces = 1, {}
    
    for s, e in function() return ensemble:find(separator, position) end do
        table.insert(pieces, ensemble:sub(position, s - 1))
        position = e + 1
    end
    table.insert(pieces, ensemble:sub(position))
    
    return pieces
end
Click to expand [+]
Alternative code that includes Explode in the string table

Example

Click to collapse [-]
Server

This example sends a welcome message to a player e.g. when joining a roleplay server.

-- get the root element
local _root = getRootElement()
-- define the onPlayerJoin handler function
function OnPlayerJoin()
    -- get the player's name
    local playername = getPlayerNametagText(source)
    -- split the player's name to first and last name
    playername = Explode(".", playername)
    -- send a welcome message
    outputChatBox("Welcome on our roleplay server, Mr./Mrs./Ms. "..playername[2]..".", source)
end
-- add the event handler
addEventHandler("onPlayerJoin", _root, OnPlayerJoin)

Author: NeonBlack