Utf8.byte: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (small tweak)
mNo edit summary
 
Line 2: Line 2:
{{Shared function}}
{{Shared function}}


Returns the code points for the i-th through j-th character of the string passed.
Returns the codepoints for the i-th through j-th character of the string passed.


==Syntax==
==Syntax==
Line 16: Line 16:


===Returns===
===Returns===
Returns a sequence of ''number'' values from the original string if successful, ''nil'' otherwise.
Returns a sequence of ''integer'' values from the original string if successful, ''nil'' otherwise.


==Example==
==Example==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example will print every code point in the input string to the server console.
This example will print every codepoint in the input string to the server console.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local input = "Ницца!"
local input = "Ницца!"
Line 40: Line 40:
</section>
</section>
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
This example will print the code point of the first character (read: 'M') in the string literal.
This example will print the codepoint of the first character (read: 'M') in the string literal.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local first = utf8.byte( "Multi Theft Auto", 1, 1 )
local first = utf8.byte( "Multi Theft Auto", 1, 1 )

Latest revision as of 18:24, 15 February 2016

Returns the codepoints for the i-th through j-th character of the string passed.

Syntax

int,... utf8.byte ( string input [, int i=1, int j=1 ] )

Required Arguments

  • input: A string character sequence

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • i: An integer representing the beginning position.
  • j: An integer representing the ending position.

Returns

Returns a sequence of integer values from the original string if successful, nil otherwise.

Example

Click to collapse [-]
Server

This example will print every codepoint in the input string to the server console.

local input = "Ницца!"
local codepoints = { utf8.byte( input, 1, utf8.len(input) ) }

for index, codepoint in ipairs( codepoints ) do
    print( "Codepoint @ ".. index .." = ".. codepoint )
end

Output:

Codepoint @ 1 = 1053
Codepoint @ 2 = 1080
Codepoint @ 3 = 1094
Codepoint @ 4 = 1094
Codepoint @ 5 = 1072
Codepoint @ 6 = 33
Click to collapse [-]
Client

This example will print the codepoint of the first character (read: 'M') in the string literal.

local first = utf8.byte( "Multi Theft Auto", 1, 1 )
outputConsole( first ) -- 77

See Also