Gettok: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(8 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | {{Server client function}} | ||
This function splits a string using the given | This function splits a string using the given separating character and returns one specified substring. | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
string gettok ( string text, int tokenNumber, int separatingCharacter ) | string gettok ( string text, int tokenNumber, string / int separatingCharacter ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''text:''' | *'''text:''' the string that should be split. | ||
*'''tokenNumber:''' | *'''tokenNumber:''' which token should be returned (1 for the first, 2 for the second, and so on). | ||
*'''separatingCharacter:''' | *'''separatingCharacter:''' the [[ASCII|ASCII number]] representing the character you want to use to separate the tokens. You can easily retrieve this by running string.byte on a string containing the separating character. | ||
===Returns=== | ===Returns=== | ||
Returns the token if it exists, ''false'' otherwise. | Returns a [[string]] containing the token if it exists, ''false'' otherwise. | ||
==Example== | ==Example== | ||
<section name="Server" class="server" show="true"> | <section name="Server" class="server" show="true"> | ||
This example retrieves the startskin and endskin for spawning a player | This example retrieves the startskin and endskin for spawning a player from a string of two numbers "a,b" | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- Put the string with the skins in a variable. Normally you would read it from a .map file or something. | -- Put the string with the skins in a variable. Normally you would read it from a .map file or something. | ||
local skins = "20,30" | local skins = "20,30" | ||
-- Get the startskin and endskin (20 and 30) | -- Get the startskin and endskin strings ("20" and "30" in this case) | ||
local startskin = gettok ( skins, 1, | local startskin = gettok ( skins, 1, ',') | ||
local endskin = gettok ( skins, 2, | local endskin = gettok ( skins, 2, ',' ) | ||
-- Get a random skin | -- Get a random skin in the range | ||
local skin = | local skin = math.random ( tonumber(startskin), tonumber(endskin) ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> | ||
Line 32: | Line 32: | ||
==See Also== | ==See Also== | ||
{{Utility functions}} | {{Utility functions}} | ||
Latest revision as of 12:48, 20 February 2019
This function splits a string using the given separating character and returns one specified substring.
Syntax
string gettok ( string text, int tokenNumber, string / int separatingCharacter )
Required Arguments
- text: the string that should be split.
- tokenNumber: which token should be returned (1 for the first, 2 for the second, and so on).
- separatingCharacter: the ASCII number representing the character you want to use to separate the tokens. You can easily retrieve this by running string.byte on a string containing the separating character.
Returns
Returns a string containing the token if it exists, false otherwise.
Example
Click to collapse [-]
ServerThis example retrieves the startskin and endskin for spawning a player from a string of two numbers "a,b"
-- Put the string with the skins in a variable. Normally you would read it from a .map file or something. local skins = "20,30" -- Get the startskin and endskin strings ("20" and "30" in this case) local startskin = gettok ( skins, 1, ',') local endskin = gettok ( skins, 2, ',' ) -- Get a random skin in the range local skin = math.random ( tonumber(startskin), tonumber(endskin) )
See Also
- addDebugHook
- base64Decode
- base64Encode
- debugSleep
- decodeString
- encodeString
- fromJSON
- generateKeyPair
- getColorFromString
- getDevelopmentMode
- getDistanceBetweenPoints2D
- getDistanceBetweenPoints3D
- getEasingValue
- getNetworkStats
- getNetworkUsageData
- getPerformanceStats
- getRealTime
- getTickCount
- getTimerDetails
- getTimers
- getFPSLimit
- getUserdataType
- getVersion
- gettok
- isTransferBoxVisible
- setTransferBoxVisible
- hash
- inspect
- interpolateBetween
- iprint
- isOOPEnabled
- isTimer
- killTimer
- md5
- passwordHash
- passwordVerify
- pregFind
- pregMatch
- pregReplace
- removeDebugHook
- resetTimer
- setDevelopmentMode
- setFPSLimit
- setTimer
- ref
- deref
- sha256
- split
- teaDecode
- teaEncode
- toJSON
- tocolor
- getProcessMemoryStats
- utfChar
- utfCode
- utfLen
- utfSeek
- utfSub
- bitAnd
- bitNot
- bitOr
- bitXor
- bitTest
- bitLRotate
- bitRRotate
- bitLShift
- bitRShift
- bitArShift
- bitExtract
- bitReplace