String length: Difference between revisions

Content added Content deleted
(→‎groovy Character Length: add recommended examples)
(→‎Lua: add unicode method)
Line 2,005: Line 2,005:


In Lua, a character is always the size of one byte so there is no difference between byte length and character length.
In Lua, a character is always the size of one byte so there is no difference between byte length and character length.

===Byte Length===
===Byte Length===

Byte length in UTF-8:

<syntaxhighlight lang="lua">str = "Hello world"
<syntaxhighlight lang="lua">str = "Hello world"
length = #str</syntaxhighlight>
length = #str</syntaxhighlight>
Line 2,015: Line 2,019:


===Character Length===
===Character Length===

Only valid for ASCII:

<syntaxhighlight lang="lua">str = "Hello world"
<syntaxhighlight lang="lua">str = "Hello world"
length = #str</syntaxhighlight>
length = #str</syntaxhighlight>
Line 2,022: Line 2,029:
<syntaxhighlight lang="lua">str = "Hello world"
<syntaxhighlight lang="lua">str = "Hello world"
length = string.len(str)</syntaxhighlight>
length = string.len(str)</syntaxhighlight>

For Unicode string, use utf8 module:

<syntaxhighlight lang="lua">
utf8.len("møøse")
utf8.len("𝔘𝔫𝔦𝔠𝔬𝔡𝔢")
utf8.len("J̲o̲s̲é̲")
</syntaxhighlight>

{{out}}

<pre>
5
7
8
</pre>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==