util.string.next
Shared
Finds the byte position and codepoint of a character relative to another
Syntax
local position, codepoint = util.string.next(
input,
start_at = 1,
offset = nil
)This function returns a byte position, not a character position.
Passing that position into util.string.sub or util.string.byte - which now index by character will give incorrect results except by coincidence on pure-ASCII input.
Parameters
| Type | Name | Description |
|---|---|---|
string | input | String to search within |
int | start_at | Byte position to search from - negative counts from end |
int | offset | Number of characters to move past start_at - defaults to 1 if start_at is given, otherwise 0 |
Returns
| Type | Name | Description |
|---|---|---|
int | nil | position | Byte position of the resulting character, or nil if out of range |
int | nil | codepoint | Codepoint of the resulting character, or nil if out of range |
Examples
local pos, code = util.string.next("hello")
core.engine.print("info", pos, code) -- 1 104local pos, code = util.string.next("hello", 1)
core.engine.print("info", pos, code) -- 2 101local pos, code = util.string.next("hello", 1, 3)
core.engine.print("info", pos, code) -- 4 108local pos, code = util.string.next("hi", 1, 5)
core.engine.print("info", pos, code) -- nil nil