Jump to content

String length: Difference between revisions

m
m (→‎{{header|UNIX Shell}}: Add note about bytes vs characters in implementations)
Line 2,054:
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
module String_length {
A$=format$("J\u0332o\u0332s\u0332e\u0301\u0332")
Print Len(A$) = 9 ' true Utf-16LE
Print Len.Disp(A$) = 49 \\ display' lengthtrue Utf-16LE
Print Len.Disp(A$) = 4 \\ display length
Buffer Clear Mem as Byte*100
\\ Write at memory at offset 0 or address Mem(0)
Return Mem, 0:=A$
Print Return Eval$(Mem, 0, 18):=A$
Print Eval$(Mem, 0, 18)
For i=0 to 17 step 2
\\ print hex value and character
Hex Eval(Mem, i as integer), ChrCode$(Eval(Mem, i as integer))
Next i
Document B$=A$
\\ encode to utf-8 with BOM (3 bytes 0xEF,0xBB,0xBF)
Save.Doc B$, "Checklen.doc", 2
Print Save.Doc B$, Filelen("Checklen.doc")=17, 2
Print Filelen("Checklen.doc")=17
\\ So length is 14 bytes + 3 the BOM
Save.Doc B$, Mem=Buffer("Checklen.doc", 2)
Print len(Mem)=17 // len works for buffers too - unit byte
// version 12 can handle strings without suffix $
C=eval$(mem, 3, 14) // from 4th byte get 14 bytes in a string
Print len(C)*2=14 ' bytes // len()) for strings return double type of words (can return 0.5)
C=string$(C as utf8dec) ' decode bytes from utf8 to utf16LE
Print len(C)=9, C=A$, Len.Disp(C)=4
Print C
Report 2, C // proportional print on console - for text center justified rendering (2 - center)
}
String_length
</syntaxhighlight>
 
404

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.