Split: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
m (Added splitMultiple info to split)
 
(31 intermediate revisions by 11 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
==Description==
{{Server client function}}
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.  
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.


For example, you could split up the string "This is a sentence", using spaces as separators. This would return "This","is","a","sentence". Each sub-string is assigned an ID number ascending from 1. In this example, ID 2 would return "is".  
'''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.


This function is commonly used to check the first word against a list of known commands. If the command is found, all subsequent words will be used as arguments.  
==Syntax==
<syntaxhighlight lang="lua">table split ( string stringToSplit, string / int separatingChar )</syntaxhighlight>
 
===Required Arguments===
* '''stringToSplit''' The string you wish to split into parts.
* '''separatingChar''' A string of the character you want to split, or the [[ASCII|ASCII number]] representing the character you want to use to split. If you want to split a string at multiple characters see [[splitMultiple]]
 
===Returns===
Returns a ''table'' of substrings split from the original string if successful, ''false'' otherwise.
 
{{note|Unicode characters work but when combined with others do not. E.g: #split("a€cb†", "€") returns 3 but #split("a€cb", "€") returns 2.
}}


==Syntax==
 
<syntaxhighlight lang="lua">string Split ( string, index, separatingchar )</syntaxhighlight>
{{note|You can't use same char twice as a separator. Eg.:  ||, ||| are the same as |.
}}


==Example==
==Example==
<syntaxhighlight lang="lua">function onPlayerChat ( player, chat )
<section name="Server" class="server" show="true">
if (split (chat, 1, " ")) == "!createhydra")
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.
  x, y, z = getPlayerPosition ( player )
<syntaxhighlight lang="lua">
  createVehicle ( 520, x + 5, y, z )
function giveWeapons(player, weaponsString)
  outputChatBox ( "You got a hydra", player )
local weaponsTable = split(weaponsString, ';') --split the string by the semi colon
end
for k,v in ipairs(weaponsTable) do --for all the split values do
end</syntaxhighlight>
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
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Utility functions}}
{{Utility functions}}

Latest revision as of 16:59, 15 November 2023

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: 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, string / int separatingChar )

Required Arguments

  • stringToSplit The string you wish to split into parts.
  • separatingChar A string of the character you want to split, or the ASCII number representing the character you want to use to split. If you want to split a string at multiple characters see splitMultiple

Returns

Returns a table of substrings split from the original string if successful, false otherwise.


[[{{{image}}}|link=|]] Note: Unicode characters work but when combined with others do not. E.g: #split("a€cb†", "€") returns 3 but #split("a€cb", "€") returns 2.


[[{{{image}}}|link=|]] Note: You can't use same char twice as a separator. Eg.:

Example

Click to collapse [-]
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

See Also