Split: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 19: Line 19:
function onConsole ( player, text )
function onConsole ( player, text )
     splittext = split ( text, 32 ) -- Grab the table of tokens
     splittext = split ( text, 32 ) -- Grab the table of tokens
     for splitKey, splitVal in splittext do -- for each token there..
     for splitKey, splitVal in ipairs(splittext) do -- for each token there..
         outputChatBox ( "key: " .. splitKey .. " val: " .. splitVal ) -- output the index and token
         outputChatBox ( "key: " .. splitKey .. " val: " .. splitVal ) -- output the index and token
     end
     end

Revision as of 22:31, 6 May 2007

This function splits a string into sub-strings. You specify a character that will act as a separating character; this will determine where to split the sub-strings. For example, it can split the string "Hello World" into two strings containing the two words, by spliting using a space as a seperating character.

Note: You can use the function gettok to retrieve a single token from the string at a specific index. This may be faster for one-off lookups, but considerably slower if you are going to check each token in a long string.

Syntax

table split ( string stringToSplit, int separatingChar )

Required Arguments

  • stringToSplit The string you wish to split into parts.
  • separatingChar The character you want to use to split (ASCII number)

Returns

Returns a table of the sub-strings.

Example

This examples takes any console text input and splits it into parts.

function onConsole ( player, text )
    splittext = split ( text, 32 ) -- Grab the table of tokens
    for splitKey, splitVal in ipairs(splittext) do -- for each token there..
        outputChatBox ( "key: " .. splitKey .. " val: " .. splitVal ) -- output the index and token
    end
end

See Also