How do I return a given character from a text?

7

How do I return a particular character from a text? For example:

text = "Eu sou vegano"

How do I detect the 5th letter of text ? If someone helps me, I'll be grateful.

    
asked by anonymous 19.12.2016 / 00:22

2 answers

7

You can use the string.sub command:

> string.sub(text, 5, 5)
o

This command receives as parameters the string, the starting position and the ending position.

If the final position is not entered, it assumes the end position of the string. Example:

> string.sub(text, 5)
ou vegano
    
19.12.2016 / 00:41
1

@Giero's answer is super correct. Additionally I recommend using a library to manipulate strings that use encodings based on their bytes.

This is not complicated, and is essential since normally only 256 character types can be represented in the Lua string type, that is, the strings contain a sequence of bytes, not characters. These bytes are considered character codes.

Multiple renderers render text based on UTF-8 text encoding. Some programming languages implement UTF-16 natively, but Lua would not be one of those.

As UTF-8 or ASCII (I think, ASCII ...) are common, it is possible for a Lua code to get characters encoded automatically through text boxes, snippets, and especially files.

So, yeah ... that's it. A string contains a collection of bytes in Lua .

When my notebook is fixed, I may try to improve my answer.

UTF-8

Version 5.3 of the Moon already offers a library ( utf8 ) to work with UTF-8 at strings, including a syntax of \u{código} almost similar to that of ECMAScript, only encoding a character in UTF-8, and only usable in strings where \ is special.

The library only does not have a sub type function, but you can make a copy using its methods:

do
    local offset, sub = utf8.offset, string.sub;

    function utf8:sub(i, j)
        i, j = tonumber(i) or 1,
               tonumber(j) or -1;

        local len = utf8.len(self);

        i, j = ( i < 0 ) and ( ( len + 1 ) + i ) or i,
            ( j < 0 ) and ( ( len + 1 ) + j ) or j;

        i, j = ( i < 1 ) and 1 or i,
            ( j > len ) and len or j;

        if ( j < i ) then
            return '';
        end

        i, j = offset(self, i),
            offset(self, j + 1);

        return sub(self, i, j - 1);
    end

end

Hence the usage looks like this:

print(
    utf8.sub('wowow䀀3', 6, 6) --> 䀀
);

Love2D also supports the same library ( documentation ), but I do not know if the Lua version is equivalent.

And there's a project on GitHub that offers a similiar library with few differences ( Stepets / utf8.lua ) and already offers a sub function.

UTF-16

One day I tried to make a library to manipulate UTF-16. It was done anyway just to see if it worked. Also it is incomplete, but when my notebook is fixed, I might recreate it: utf16 .

For now I do not know any other codings ..

    
01.01.2017 / 13:16