Compare length of two strings: Difference between revisions

Line 109:
length(codeunits("ñññ")) > length(codeunits("nnnn")) # true as well
</lang>
 
=={{header|Nim}}==
In Nim, a character (<code>char</code>) is represented on a byte. A string is a sequence of characters with a length. For interoperability reason, an extra null is added at the end of the characters.
A string is supposed to be encoded in UTF-8, but this is not enforced. The function <code>len</code> returns the length of the string i.e. its number of characters (without the extra null).
 
If we want to manage a string as a Unicode sequence of code points, we have to use the module <code>unicode</code>. We can convert a string in a sequence of runes, each rune being a unicode UTF-32 value. The length of this sequence is the number of code points.
 
<lang Nim>import strformat, unicode
 
const
S1 = "marche"
S2 = "marché"
 
echo &"“{S2}”, byte length = {S2.len}, code points: {S2.toRunes.len}"
echo &"“{S1}”, byte length = {S1.len}, code points: {S1.toRunes.len}"</lang>
 
{{out}}
<pre>“marché”, byte length = 7, code points: 6
“marche”, byte length = 6, code points: 6</pre>
 
=={{header|Raku}}==
Anonymous user