Compare length of two strings: Difference between revisions

julia example
m (Took feedback and updated task accordingly.)
(julia example)
Line 11:
{{Template:Strings}}
<br><br>
 
 
=={{header|Julia}}==
Per the Julia docs, a String in Julia is a sequence of charachters encoded as UTF-8.
Most string methods in Julia actually accept an AbstractString, which is the supertype
of strings in Julia regardless of the encoding, including the default UTF-8.
 
The Char data type in Julia is a 32-bit, potentially Unicode data type, so that if we enumerate a String
as a Char array, we get a series of 32-bit characters:
<lang julia>s = "niño"
println("Position Char Bytes\n==============================")
for (i, c) in enumerate(s)
println("$i $c $(sizeof(c))")
end
</lang>{{out}}
<pre>
Position Char Bytes
==============================
1 n 4
2 i 4
3 ñ 4
4 o 4
</pre>
 
However, if we index into the string, the index into the string will function
as if the string was an ordinary C string, that is, an array of unsigned 8-bit
integers. If the index attempts to index within a character of size greater than
one byte, an error is thrown for bad indexing. This can be demonstrated by casting
the above string to codeunits:
<lang julia>println("Position Codeunit Bytes\n==============================")
for (i, c) in enumerate(codeunits(s))
println("$i $(string(c, base=16)) $(sizeof(c))")
end
</lang>{{out}}
<pre>
Position Codeunit Bytes
==============================
1 6e 1
2 69 1
3 c3 1
4 b1 1
5 6f 1
</pre>
 
Note that the length of "niño" as a String is 4 characters, and the length
of "niño" as codeunits (ie, 8 bit bytes) is 5. Indexing into the 4th position
results in an error:
<lang julia>
julia> s[4]
ERROR: StringIndexError: invalid index [4], valid nearby indices [3]=>'ñ', [5]=>'o'
</lang>
 
So, whether a string is longer or shorter depends on the encoding, as below:
<lang julia>length("ñññ") < length("nnnn") # true, and the usual meaning of length of a String
 
length(codeunits("ñññ")) > length(codeunits("nnnn")) # true as well
</lang>
 
 
=={{header|Raku}}==
4,108

edits