Utf8.charpos: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ {{Shared function}} Converts the UTF-8 code point position to byte-string position. {{Note|Code point characters beyond the byte value range (0-127) require at leas...") |
mNo edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
{{Shared function}} | {{Shared function}} | ||
Converts the UTF-8 | Converts the UTF-8 codepoint position to byte-string position. | ||
{{Note|Code point characters beyond the byte value range (0-127) require at least 2 bytes to represent the character}} | {{Note|Code point characters beyond the byte value range (0-127) require at least 2 bytes to represent the character}} | ||
Line 13: | Line 13: | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
{{OptionalArg}} | {{OptionalArg}} | ||
*'''charpos:''' | *'''charpos:''' An integer representing the beginning position (offset will be added/subtracted). | ||
*'''offset:''' | *'''offset:''' An integer representing the offset to charpos. | ||
===Returns=== | ===Returns=== | ||
Returns the '' | Returns the ''integer'' position as in a byte string and the ''integer'' codepoint at this position, ''nil'' otherwise. | ||
==Example== | ==Example== | ||
<section name="Server" class="server" show="true"> | <section name="Server" class="server" show="true"> | ||
This example takes the second | This example takes the second codepoint character and shows the byte-string position and the codepoint character code. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local position, codepoint = utf8.charpos( "Привет", 2 ) | local position, codepoint = utf8.charpos( "Привет", 2 ) |
Latest revision as of 18:27, 15 February 2016
Converts the UTF-8 codepoint position to byte-string position.
Syntax
int, int utf8.charpos ( string input [[, int charpos = 0 ], int offset = 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.
- charpos: An integer representing the beginning position (offset will be added/subtracted).
- offset: An integer representing the offset to charpos.
Returns
Returns the integer position as in a byte string and the integer codepoint at this position, nil otherwise.
Example
Click to collapse [-]
ServerThis example takes the second codepoint character and shows the byte-string position and the codepoint character code.
local position, codepoint = utf8.charpos( "Привет", 2 ) print( position, codepoint ) -- 3, 1088
Click to collapse [-]
ClientThis example extracts the first character by calculating the character length with the UTF8 functions and the inbuilt Lua function string.sub, which processes byte strings.
local input = "Привет мир" -- Hello World local from = utf8.charpos( input, 1 ) -- 1 local to = utf8.charpos( input, 2 ) -- 3 local byteLength = to - from outputConsole( byteLength ) -- 2 local character = string.sub( input, from, byteLength ) outputConsole( character ) -- П
See Also
- utf8.byte
- utf8.char
- utf8.charpos
- utf8.escape
- utf8.find
- utf8.fold
- utf8.gmatch
- utf8.gsub
- utf8.insert
- utf8.len
- utf8.lower
- utf8.match
- utf8.ncasecmp
- utf8.next
- utf8.remove
- utf8.reverse
- utf8.sub
- utf8.title
- utf8.upper
- utf8.width
- utf8.widthindex