split | Multi Theft Auto: Wiki Skip to content

split

Client-side
Server-side
Shared

This page is incomplete! Help wanted!

Please finish this page using the corresponding Old Wiki article.
Go to Contribution guidelines for more information.


This function splits a string into substrings. 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 separator.

Note

Unicode characters work but when combined with others do not. E.g: #split("a€cb†", "€") returns 3 but #split("a€cb", "€") returns 2.

Note

You can't use same char twice as a separator. Eg.:

Syntax

split ( )

Code Examples

server

This example gives the specified weapons to the given player, while the weapons are a string in the form: 'weaponId,ammo;weaponId2,ammo2;weaponId3,ammo3;..'. This is especially for data read from a .map file attribute.

function giveWeapons(player, weaponsString)
local weaponsTable = split(weaponsString, ';') --split the string by the semi colon
for k,v in ipairs(weaponsTable) do --for all the split values do
weaponId = gettok(v, 1, string.byte(',')) --get the weapon ID using gettok, retrieve the first token
weaponAmmo = gettok(v, 2, ",") --get the ammo using gettok, retrieve the second token
if (weaponId and weaponAmmo) then --if neither of them is invalid
giveWeapon(player, weaponId, weaponAmmo) --give the player the weapons
end
end
end