String length: Difference between revisions
Content added Content deleted
(→{{header|Julia}}: add Grapheme Length and use suggested examples) |
(→{{header|Nim}}: use common examples) |
||
Line 2,227: | Line 2,227: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
In Nim, <code>len</code> returns the byte length of strings, ignoring the UTF-8 encoding. When dealing with Unicode strings, the module <code>unicode</code> must be used. |
|||
⚫ | |||
===Byte Length=== |
|||
var s: string = "Hello, world! ☺" |
|||
⚫ | |||
echo &"“{s}” has byte length {s.len}." |
|||
echo "møøse".len # 7 |
|||
echo &"“{s}” has Unicode char length {s.runeLen}."</syntaxhighlight> |
|||
echo "𝔘𝔫𝔦𝔠𝔬𝔡𝔢".len # 28 |
|||
echo "J̲o̲s̲é̲".len # 13 |
|||
</syntaxhighlight> |
|||
===Character Length=== |
|||
{{out}} |
|||
<pre>“Hello, world! ☺” has byte length 17. |
|||
<syntaxhighlight lang="nim"> |
|||
“Hello, world! ☺” has Unicode char length 15.</pre> |
|||
import unicode |
|||
echo "møøse".runeLen # 5 |
|||
echo "𝔘𝔫𝔦𝔠𝔬𝔡𝔢".runeLen # 7 |
|||
echo "J̲o̲s̲é̲".runeLen # 8 |
|||
</syntaxhighlight> |
|||
===Grapheme Length=== |
|||
[https://nim-lang.org/docs/unicode.html#graphemeLen%2Cstring%2CNatural graphemeLen()] does not do what you expect. It doesn't return the number of grapheme in a string but returns the number of bytes at a character/codepoint index for a given string. |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |