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 anyone would help me, I’d be grateful.
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 anyone would help me, I’d be grateful.
7
You can use the command string.sub
:
> string.sub(text, 5, 5)
o
This command takes as parameters the string, the initial position and the final position.
If the final position is not reported, it assumes the final position of the string. Example:
> string.sub(text, 5)
ou vegano
1
The reply of @Gomiero is super correct. Additionally, I recommend using a library to manipulate strings that use byte-based encodings.
This is not complicated, and is essential since usually only 256 character types can be represented in the Lua string type, that is, the strings contain a string of bytes, not characters. These bytes are considered character codes.
Several renderers render texts based on UTF-8 text encoding. Some programming languages implement UTF-16 natively, but Lua would not be one of these.
As UTF-8 or ASCII (I think, ASCII...) are common, it is possible for a code in Lua to obtain characters encoded automatically through text boxes, snippets, and mainly files.
So, yes... that’s it. A string contains a collection of Lua bytes.
When my laptop is fixed, maybe I’ll try to improve my answer.
Version 5.3 of Lua already offers a library (utf8) to work with UTF-8 strings, including a syntax of \u{código}
almost similar to Ecmascript, only encoding a character in UTF-8, and only usable in strings where it is special.
The library only has 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
Then the use goes like this:
print(
utf8.sub('wowow䀀3', 6, 6) --> 䀀
);
Love2d also supports the same library (documentation), but I don’t know if the Lua version is equivalent.
And there’s a project on Github that offers a similar library, with few differences (Stepets/utf8.moon) and already offers a sub function.
One day I tried to make a library to manipulate UTF-16. It was made anyway just to see if it worked. Besides, it’s incomplete, but when my laptop gets fixed, maybe I’ll recreate it: utf16.
So far I don’t know any other encodings..
Browser other questions tagged lua
You are not signed in. Login or sign up in order to post.
thanks :) now I can make the script to animate a text as if I were speaking in an RPG style
– arthurgps2
@arthurgps2 You’re welcome!!
– Gomiero