String length: Difference between revisions

Content added Content deleted
No edit summary
(Merged the two programs in a single one. Added output.)
Line 2,076: Line 2,076:


=={{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===
<lang Nim>var s: string = "Hello, world! ☺"
<lang Nim>import strformat, unicode
echo '"',s, '"'," has byte length: ", len(s)


# -> "Hello, world! ☺" has unicode char length: 17</lang>
var s: string = "Hello, world! ☺"


echo &"“{s}” has byte length {s.len}."
===Character Length===
echo &"“{s}” has Unicode char length {s.runeLen}."</lang>
<lang Nim>import unicode

var s: string = "Hello, world! ☺"
echo '"',s, '"'," has unicode char length: ", runeLen(s)


{{out}}
# -> "Hello, world! ☺" has unicode char length: 15</lang>
<pre>“Hello, world! ☺” has byte length 17.
“Hello, world! ☺” has Unicode char length 15.</len>


=={{header|Oberon-2}}==
=={{header|Oberon-2}}==