String length: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 260:
<lang axe>"HELLO, WORLD"→Str1
Disp length(Str1)▶Dec,i</lang>
 
=={{header|Batch File}}==
===Byte Length===
<lang dos>@echo off
setlocal enabledelayedexpansion
call :length %1 res
echo length of %1 is %res%
goto :eof
 
:length
set str=%~1
set cnt=0
:loop
if "%str%" equ "" (
set %2=%cnt%
goto :eof
)
set str=!str:~1!
set /a cnt = cnt + 1
goto loop</lang>
 
=={{header|BaCon}}==
Line 358 ⟶ 338:
<lang IS-BASIC>100 INPUT PROMPT "String: ":TX$
110 PRINT LEN(TX$)</lang>
 
=={{header|Batch File}}==
===Byte Length===
<lang dos>@echo off
setlocal enabledelayedexpansion
call :length %1 res
echo length of %1 is %res%
goto :eof
 
:length
set str=%~1
set cnt=0
:loop
if "%str%" equ "" (
set %2=%cnt%
goto :eof
)
set str=!str:~1!
set /a cnt = cnt + 1
goto loop</lang>
 
=={{header|BBC BASIC}}==
Line 508:
}</lang>output<pre>bytes: 7
chars: 5</pre>
 
=={{header|C sharp|C#}}==
'''Platform:''' [[.NET]]
 
{{works with|C sharp|C #|1.0+}}
===Character Length===
<lang csharp>string s = "Hello, world!";
int characterLength = s.Length;</lang>
 
===Byte Length===
Strings in .NET are stored in Unicode.
<lang csharp>using System.Text;
 
string s = "Hello, world!";
int byteLength = Encoding.Unicode.GetByteCount(s);</lang>
To get the number of bytes that the string would require in a different encoding, e.g., UTF8:
<lang csharp>int utf8ByteLength = Encoding.UTF8.GetByteCount(s);</lang>
 
=={{header|C++}}==
Line 619 ⟶ 636:
 
Note that the strings are given as explicit hex sequences, so that the encoding used for the source code won't matter.
 
=={{header|C sharp|C#}}==
'''Platform:''' [[.NET]]
 
{{works with|C sharp|C #|1.0+}}
===Character Length===
<lang csharp>string s = "Hello, world!";
int characterLength = s.Length;</lang>
 
===Byte Length===
Strings in .NET are stored in Unicode.
<lang csharp>using System.Text;
 
string s = "Hello, world!";
int byteLength = Encoding.Unicode.GetByteCount(s);</lang>
To get the number of bytes that the string would require in a different encoding, e.g., UTF8:
<lang csharp>int utf8ByteLength = Encoding.UTF8.GetByteCount(s);</lang>
 
=={{header|Clean}}==
Line 905:
===Character Length===
<lang e>"Hello World".size()</lang>
 
=={{header|Elena}}==
===Character Length===
Line 1,285 ⟶ 1,286:
=={{header|HicEst}}==
<lang hicest>LEN("1 character == 1 byte") ! 21</lang>
=={{header|Icon}} and {{header|Unicon}}==
==== Character Length ====
<lang Icon> length := *s</lang>
 
Note: Neither Icon nor Unicon currently supports double-byte character sets.
 
=={{header|HolyC}}==
Line 1,296 ⟶ 1,292:
Print("%d\n", StrLen(string));
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
==== Character Length ====
<lang Icon> length := *s</lang>
 
Note: Neither Icon nor Unicon currently supports double-byte character sets.
 
=={{header|IDL}}==
Line 1,424 ⟶ 1,426:
"length of 𝔘𝔫𝔦𝔠𝔬𝔡𝔢 is 7"</lang>
 
=={{header|KotlinJudoScript}}==
===Byte Length===
As in Java, a string in Kotlin is essentially a sequence of UTF-16 encoded characters and the 'length' property simply returns the number of such characters in the string. Surrogates or graphemes are not treated specially for this purpose - they are just represented by the appropriate number of UTF-16 characters.
{{needs-review|JudoScript}}
 
<lang judoscript>//Store length of hello world in length and print it
As each UTF-16 character occupies 2 bytes, it follows that the number of bytes occupied by the string will be twice the length:
. length = "Hello World".length();</lang>
<lang scala>// version 1.0.6
===Character Length===
fun main(args: Array<String>) {
{{needs-review| JudoScript}}
val s = "José"
<lang judoscript>//Store length of hello world in length and print it
println("The char length is ${s.length}")
. length = "Hello World".length()</lang>
println("The byte length is ${Character.BYTES * s.length}")
}</lang>
 
{{out}}
<pre>
The char length is 4
The byte length is 8
</pre>
 
=={{header|Julia}}==
Line 1,451 ⟶ 1,446:
<lang julia>length("Hello, world!") # gives 13
length("Hellö, wørld!") # gives 13</lang>
 
=={{header|JudoScript}}==
===Byte Length===
{{needs-review|JudoScript}}
<lang judoscript>//Store length of hello world in length and print it
. length = "Hello World".length();</lang>
===Character Length===
{{needs-review| JudoScript}}
<lang judoscript>//Store length of hello world in length and print it
. length = "Hello World".length()</lang>
 
=={{header|K}}==
Line 1,470 ⟶ 1,455:
13
</lang>
 
=={{header|Kotlin}}==
As in Java, a string in Kotlin is essentially a sequence of UTF-16 encoded characters and the 'length' property simply returns the number of such characters in the string. Surrogates or graphemes are not treated specially for this purpose - they are just represented by the appropriate number of UTF-16 characters.
 
As each UTF-16 character occupies 2 bytes, it follows that the number of bytes occupied by the string will be twice the length:
<lang scala>// version 1.0.6
fun main(args: Array<String>) {
val s = "José"
println("The char length is ${s.length}")
println("The byte length is ${Character.BYTES * s.length}")
}</lang>
 
{{out}}
<pre>
The char length is 4
The byte length is 8
</pre>
 
=={{header|LabVIEW}}==
Line 1,479 ⟶ 1,481:
 
[[File:LV strlen.png]]
 
 
=={{header|Lasso}}==
Line 1,628 ⟶ 1,629:
===Character Length===
<lang maxscript>"Hello world".count</lang>
 
 
=={{header|Mercury}}==
Line 1,769:
IO.Put("String length of s: " & Fmt.Int(Text.Length(s)) & "\n");
END StringLength.</lang>
 
=={{header|NewLISP}}==
===Character Length===
<lang NewLISP>(set 'Str "møøse")
(println Str " is " (length Str) " characters long")</lang>
 
=={{header|Nemerle}}==
Line 1,786 ⟶ 1,781:
def message = "How long am I anyways?";
def bytelength = Encoding.Unicode.GetByteCount(message);</lang>
 
=={{header|NewLISP}}==
===Character Length===
<lang NewLISP>(set 'Str "møøse")
(println Str " is " (length Str) " characters long")</lang>
 
=={{header|Nim}}==
Line 1,845:
Length: 3
</pre>
 
=={{header|Objeck}}==
All character string elements are 1-byte in size therefore a string's byte size and length are the same.
 
===Character Length===
<lang objeck>
"Foo"->Size()->PrintLine();
</lang>
 
===Byte Length===
<lang objeck>
"Foo"->Size()->PrintLine();
</lang>
 
=={{header|Objective-C}}==
Line 1,872 ⟶ 1,885:
unsigned numberOfBytes =
[@"møøse" lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; // 7</lang>
 
=={{header|Objeck}}==
All character string elements are 1-byte in size therefore a string's byte size and length are the same.
 
===Character Length===
<lang objeck>
"Foo"->Size()->PrintLine();
</lang>
 
===Byte Length===
<lang objeck>
"Foo"->Size()->PrintLine();
</lang>
 
=={{header|OCaml}}==
Line 1,919:
 
This gives the number of bytes, not of characters. e.g. length("è") is 2 when "è" is encoded e.g. as UTF-8.
 
 
=={{header|Oforth}}==
Line 1,961 ⟶ 1,960:
===Byte Length===
<lang oz>{Show {Length "Hello World"}}</lang>
Oz uses a single-byte encoding by default. So for normal strings, this will also show the correct character length.
 
=={{header|PARI/GP}}==
Line 2,019 ⟶ 2,018:
{{out}}
<pre>Grapheme length: 2</pre>
 
=={{header|Perl 6}}==
===Byte Length===
 
<lang perl6>say 'møøse'.encode('UTF-8').bytes;</lang>
 
===Character Length===
 
<lang perl6>say 'møøse'.codes;</lang>
 
===Grapheme Length===
 
<lang perl6>say 'møøse'.chars;</lang>
 
=={{header|Phix}}==
Line 2,286 ⟶ 2,272:
<lang Racket>-> (printf "str has ~a bytes in utf-8" (bytes-length (string->bytes/utf-8 str)))
str has 14 bytes in utf-8</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
===Byte Length===
 
<lang perl6>say 'møøse'.encode('UTF-8').bytes;</lang>
 
===Character Length===
 
<lang perl6>say 'møøse'.codes;</lang>
 
===Grapheme Length===
 
<lang perl6>say 'møøse'.chars;</lang>
 
=={{header|REBOL}}==
Line 2,446:
<lang runbasic>input a$
print len(a$)</lang>
 
=={{header|SAS}}==
<lang sas>data _null_;
a="Hello, World!";
b=length(c);
put _all_;
run;</lang>
 
=={{header|Rust}}==
Line 2,469 ⟶ 2,462:
}
</lang>
 
=={{header|SAS}}==
<lang sas>data _null_;
a="Hello, World!";
b=length(c);
put _all_;
run;</lang>
 
=={{header|Scala}}==
{{libheader|Scala}}
<lang scala>
object StringLength extends App {
val s1 = "møøse"
val s3 = List("\uD835\uDD18", "\uD835\uDD2B", "\uD835\uDD26",
"\uD835\uDD20", "\uD835\uDD2C", "\uD835\uDD21", "\uD835\uDD22").mkString
val s4 = "J\u0332o\u0332s\u0332e\u0301\u0332"
 
List(s1, s3, s4).foreach(s => println(
s"The string: $s, characterlength= ${s.length} UTF8bytes= ${
s.getBytes("UTF-8").size
} UTF16bytes= ${s.getBytes("UTF-16LE").size}"))
}
</lang>
{{out}}
<pre>The string: møøse, characterlength= 5 UTF8bytes= 7 UTF16bytes= 10
The string: 𝔘𝔫𝔦𝔠𝔬𝔡𝔢, characterlength= 14 UTF8bytes= 28 UTF16bytes= 28
The string: J̲o̲s̲é̲, characterlength= 9 UTF8bytes= 14 UTF16bytes= 18</pre>
 
=={{header|Scheme}}==
Line 2,528 ⟶ 2,548:
===Grapheme Length===
<lang ruby>say str.graphs.len; #=> 4</lang>
 
=={{header|Simula}}==
Simula has no bultin support for character encodings (Unicode was not even invented in the year 1967). The encoding was regarded responsibility of the operating system and one byte must match one character.
Line 2,617 ⟶ 2,638:
"€" CHARACTER LENGTH = 1
</pre>
 
=={{header|Scala}}==
{{libheader|Scala}}
<lang scala>
object StringLength extends App {
val s1 = "møøse"
val s3 = List("\uD835\uDD18", "\uD835\uDD2B", "\uD835\uDD26",
"\uD835\uDD20", "\uD835\uDD2C", "\uD835\uDD21", "\uD835\uDD22").mkString
val s4 = "J\u0332o\u0332s\u0332e\u0301\u0332"
 
List(s1, s3, s4).foreach(s => println(
s"The string: $s, characterlength= ${s.length} UTF8bytes= ${
s.getBytes("UTF-8").size
} UTF16bytes= ${s.getBytes("UTF-16LE").size}"))
}
</lang>
{{out}}
<pre>The string: møøse, characterlength= 5 UTF8bytes= 7 UTF16bytes= 10
The string: 𝔘𝔫𝔦𝔠𝔬𝔡𝔢, characterlength= 14 UTF8bytes= 28 UTF16bytes= 28
The string: J̲o̲s̲é̲, characterlength= 9 UTF8bytes= 14 UTF16bytes= 18</pre>
 
=={{header|Slate}}==
Line 3,027 ⟶ 3,028:
===Byte Length===
<lang trith>"møøse" size</lang>
 
=={{header|TUSCRIPT}}==
===Character Length ===
Line 3,233 ⟶ 3,235:
bytes (UTF-32) 36
</pre>
 
=={{header|Wren}}==
===Byte Length===
<lang wren>System.print("møøse".bytes.count)
System.print("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".bytes.count)
System.print("J̲o̲s̲é̲".bytes.count)
</lang>
===Character Length===
<lang wren>System.print("møøse".count)
System.print("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".count)
System.print("J̲o̲s̲é̲".count)
</lang>
 
=={{header|x86 Assembly}}==
Line 3,296 ⟶ 3,310:
 
<lang xtalk>put the number of characters in "Hello World"</lang>
 
=={{header|Wren}}==
===Byte Length===
<lang wren>System.print("møøse".bytes.count)
System.print("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".bytes.count)
System.print("J̲o̲s̲é̲".bytes.count)
</lang>
===Character Length===
<lang wren>System.print("møøse".count)
System.print("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".count)
System.print("J̲o̲s̲é̲".count)
</lang>
 
=={{header|Yorick}}==
10,333

edits