String Byte Length: Difference between revisions
Content added Content deleted
mNo edit summary |
m (Stupid case-sensitivity.) |
||
(28 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[String length]] |
|||
[http://beyonddreamingx.angelfire.com/content/scanjet.htm scanjet 3800] [http://alena344.ifrance.com/text/the-sims/ the sims hot date] [http://jrosestar.ifrance.com/html/luc-merenda/ luc merenda] [http://isabellaval.ifrance.com/elenchi/ elenchi] [http://mckueltzi.at.tut.by/description/lib/cartoleria-vicenza.htm cartoleria vicenza] [http://breveret.at.tut.by/view/new/giubbotti-dolce.htm giubbotti dolce e gabbana] [http://nymphbmbzl222.ifrance.com/piolet.htm piolet] [http://zaweaver.at.tut.by/text/directory/radar-detector.htm radar detector] [http://habeard.angelfire.com/topic/canalis-corvaglia.htm canalis corvaglia max] [http://lidresze.angelfire.com/blog/sabrina-salermo.htm sabrina salermo] [http://beyonddreamingx.angelfire.com/content/solo-tu.htm solo tu] [http://lasylvan.angelfire.com/new/bigclarence/ bigclarence] [http://kairikekui.ifrance.com/resources/www-brutalidades/ www brutalidades com] [http://konstantinkar.ifrance.com/description/narciso-parisi/ narciso parisi] [http://mcunderh.at.tut.by/resources/articles/norton-internet.htm norton internet 2006] [http://konstantinkar.ifrance.com/description/intercettazioni-telefonica/ intercettazioni telefonica] [http://dweejah.ifrance.com/new/assplundering-it/ assplundering it] [http://dweejah.ifrance.com/new/lisi/ lisi] [http://dweejah.ifrance.com/new/sodimm-gb/ sodimm 1gb] [http://isabellaval.ifrance.com/canon-powershot/ canon powershot] [http://konstantinkar.ifrance.com/description/pan-kimono/ pan kimono] [http://gaburlei.angelfire.com/resources/bagutti-liscio.htm bagutti liscio] [http://lasylvan.angelfire.com/new/gioco-biliardo/ gioco biliardo on line] [http://jrosestar.ifrance.com/html/il-papa/ il papa buono] [http://sagaddy.angelfire.com/styles/finanziamento-caorle/ finanziamento caorle] [http://dweejah.ifrance.com/new/o-zone/ o zone karaoke] [http://emstripl.at.tut.by/view/new/ack.htm ack 700] [http://breveret.at.tut.by/view/new/cavallari.htm cavallari] [http://ariadnafeni.ifrance.com/resources/mariangela-d/ mariangela d abbraccio] [http://dylantian.ifrance.com/styles/only-me.htm only me] [http://rebuckma.angelfire.com/description/casse-indiana.htm casse indiana line] [http://konstantinkar.ifrance.com/description/jeans-dolce/ jeans dolce e gabbana uomo] [http://dylantian.ifrance.com/styles/intimo-maschile.htm intimo maschile sexy] [http://lasylvan.angelfire.com/new/sabc-com/ sabc com za] [http://isabellaval.ifrance.com/cartoline-pubblicitarie/ cartoline pubblicitarie] [http://isabellaval.ifrance.com/trac-trac/ trac trac] [http://alena344.ifrance.com/text/cl/ cl 2000] [http://macertot.ifrance.com/view/canal-codes.htm canal codes] [http://alena344.ifrance.com/text/orgasmos-de/ orgasmos de teens] [http://pohogue.angelfire.com/library/scanner-hp.htm scanner hp scanjet 5530c] [http://wifry.angelfire.com/styles/l-carnitina.htm l carnitina] [http://sagaddy.angelfire.com/styles/gun-con/ gun con 2] [http://casloan.angelfire.com/small/dulcolax-ad/ dulcolax ad 6 sup 10 mg] [http://mcunderh.at.tut.by/resources/articles/svizzera-cose.htm svizzera cose fare] [http://jrosestar.ifrance.com/html/computer-pentium/ computer pentium 4 630] [http://konstantinkar.ifrance.com/description/memoire-dess/ memoire dess social] [http://casloan.angelfire.com/small/ef/ ef 90 300] [http://lasylvan.angelfire.com/new/tv-card/ tv card] [http://beyonddreamingx.angelfire.com/content/honda-black.htm honda black widow] [http://crhighto.at.tut.by/img/styles/creare-giochi.htm creare giochi] [http://lasylvan.angelfire.com/new/nizip/ nizip] [http://wifry.angelfire.com/styles/the-farm.htm the farm rebecca] [http://ariadnafeni.ifrance.com/resources/cos-e/ cos e evento segreto] [http://thmastrie.angelfire.com/resources/o-zone.htm o zone mai la hii] [http://wiwoolle.at.tut.by/content/view/james-getting.htm james getting away] [http://habeard.angelfire.com/topic/masterizzatori-combo.htm masterizzatori combo per notebook] [http://beyonddreamingx.angelfire.com/content/mail-lite.htm mail lite] [http://ariadnafeni.ifrance.com/resources/video-musicali/ video musicali on lione] [http://alena344.ifrance.com/text/danisko/ danisko] [http://nymphbmbzl222.ifrance.com/madre-e.htm madre e figli che scopano] [http://dweejah.ifrance.com/new/uil-di/ uil di chieti] [http://wifry.angelfire.com/styles/bakane.htm bakane] [http://ariadnafeni.ifrance.com/resources/stampaggio-plastica/ stampaggio plastica bologna] [http://wiwoolle.at.tut.by/content/view/hard-boiled.htm hard boiled] [http://mcunderh.at.tut.by/resources/articles/le-avventure.htm le avventure amorose di robin hood] [http://agodichik.angelfire.com/styles/and-tai.htm and1 tai chi mid] [http://habeard.angelfire.com/topic/gigabyte-gaknsc.htm gigabyte gak8nsc] [http://kairikekui.ifrance.com/resources/agriturismo-costiera/ agriturismo costiera amalfitana] [http://jrosestar.ifrance.com/html/dimagrire-gambe/ dimagrire gambe] [http://rebuckma.angelfire.com/description/turbina-flow.htm turbina flow meter] [http://hehalley.at.tut.by/content/view/calcio-spagnolo.htm calcio spagnolo] [http://isabellaval.ifrance.com/come-si/ come si fa il fumo] [http://jrosestar.ifrance.com/html/www-dragonball/ www dragonball hf it] [http://casloan.angelfire.com/small/pickup-tata/ pickup tata] [http://konstantinkar.ifrance.com/description/mary-gemeli/ mary gemeli diversi] [http://wiwoolle.at.tut.by/content/view/mario-party.htm mario party advance] [http://gaburlei.angelfire.com/resources/macromedia-studio.htm macromedia studio 2004] [http://habeard.angelfire.com/topic/prestito-cremona.htm prestito cremona] [http://kataevka.ifrance.com/articles/gay-movies/ gay movies sex free] [http://isabellaval.ifrance.com/livellatrice/ livellatrice] [http://isabellaval.ifrance.com/toca-world/ toca world touring cars] [http://brandalinden.ifrance.com/topic/balestrino.htm balestrino] [http://nymphbmbzl222.ifrance.com/il-killer.htm il killer dagli occhi a mandorla] [http://kedewber.at.tut.by/topic/hai-phong.htm hai phong] [http://jrosestar.ifrance.com/html/snav/ snav] [http://mckueltzi.at.tut.by/description/lib/caslinghe.htm caslinghe] [http://nymphbmbzl222.ifrance.com/rds-portatile.htm rds portatile] [http://lidresze.angelfire.com/blog/gaussian.htm gaussian94] [http://hewvey.ifrance.com/library/porno-anime/ porno anime] [http://wifry.angelfire.com/styles/happy-profumo.htm happy profumo clinique] [http://macertot.ifrance.com/view/alfa-connect.htm alfa connect] [http://feralpaw-omm.ifrance.com/html/dlink-dwlg/ dlink dwlg650] [http://feralpaw-omm.ifrance.com/html/hp/ hp 7171] [http://pohogue.angelfire.com/library/michael-radford.htm michael radford] [http://isabellaval.ifrance.com/telecamera-ip/ telecamera ip] [http://mcunderh.at.tut.by/resources/articles/audi-a.htm audi a4 25 tdi avant] [http://kedewber.at.tut.by/topic/bios-pcsx.htm bios pcsx2] [http://dweejah.ifrance.com/new/ti-ni/ ti ni si znala] [http://kibrumfik.at.tut.by/text/directory/parliamo-al.htm parliamo al singolare] [http://beyonddreamingx.angelfire.com/content/oligominerale.htm oligominerale] [http://habeard.angelfire.com/topic/scott-peterson.htm scott peterson trial] [http://hewvey.ifrance.com/library/magia-bianca/ magia bianca] [http://macertot.ifrance.com/view/licenza-windows.htm licenza windows 2003 server] [http://casloan.angelfire.com/small/frasi-d/ frasi d amore oscar wilde e william shakespere] [http://dweejah.ifrance.com/new/io-tigro/ io tigro tu tigri egli tigra] [http://casloan.angelfire.com/small/www-decorazioni/ www decorazioni natalizie it] [http://rebuckma.angelfire.com/description/tastiera-bluetooth.htm tastiera bluetooth pocket pc] [http://brandalinden.ifrance.com/topic/zorba-greek.htm zorba greek] [http://habeard.angelfire.com/topic/il-padrino.htm il padrino di hong kong] [http://konstantinkar.ifrance.com/description/legno-it/ legno it] [http://hehalley.at.tut.by/content/view/free-chat.htm free chat line] [http://rebuckma.angelfire.com/description/pinguino.htm pinguino] [http://dweejah.ifrance.com/new/vigo-roma/ vigo roma] [http://gaburlei.angelfire.com/resources/modem-modem.htm modem modem adsl] [http://jrosestar.ifrance.com/html/kill-santa/ kill santa claus] [http://isabellaval.ifrance.com/pact-the/ pact the] [http://emstripl.at.tut.by/view/new/www-anas.htm www anas it] [http://feralpaw-omm.ifrance.com/html/ben-lee/ ben lee] [http://thmastrie.angelfire.com/resources/largine.htm largine] [http://nymphbmbzl222.ifrance.com/stefano-pettini.htm stefano pettini] [http://konstantinkar.ifrance.com/description/tasto-nokia/ tasto nokia 6600] [http://sagaddy.angelfire.com/styles/sexy-ragazze/ sexy ragazze] [http://jrosestar.ifrance.com/html/allison-pisa/ allison pisa] [http://ariadnafeni.ifrance.com/resources/impara-a/ impara a conoscere il tuo coniglio] [http://dweejah.ifrance.com/new/hedvika-koller/ hedvika koller] [http://beyonddreamingx.angelfire.com/content/acer-centrino.htm acer centrino 8101wlmi] [http://dweejah.ifrance.com/new/tecnico-in/ tecnico in igiene ambientale del lavoro] [http://spkleins.angelfire.com/ford-lombardia/ ford lombardia] [http://wipitts.at.tut.by/text/directory/riolo-terme.htm riolo terme] [http://agodichik.angelfire.com/styles/batterie-videocamera.htm batterie videocamera panasonic] [http://seedging.at.tut.by/text/directory/materiel-de.htm materiel de nettoyage] [http://beyonddreamingx.angelfire.com/content/hacer-el.htm hacer el amor con otro] [http://hewvey.ifrance.com/library/asus-opteron/ asus opteron] [http://habeard.angelfire.com/topic/sogni-di.htm sogni di donna] [http://jrosestar.ifrance.com/html/elenco-pianisti/ elenco pianisti piano bar messina] [http://dylantian.ifrance.com/styles/oklahoma-cose.htm oklahoma cose fare] [http://crhighto.at.tut.by/img/styles/brutta-troia.htm brutta troia] [http://sagaddy.angelfire.com/styles/c/ c2 1 4] [http://yvharlen.at.tut.by/lib/everybody-chang.htm everybody chang dei keane] [http://konstantinkar.ifrance.com/description/firenze-milano/ firenze milano biglietti aerei] [http://kairikekui.ifrance.com/resources/sony-/ sony psj20 s] [http://pohogue.angelfire.com/library/polarizzatore-hoya.htm polarizzatore hoya] [http://ferandol.at.tut.by/images/small/giochi-computer.htm giochi computer scaricare] [http://zaweaver.at.tut.by/text/directory/converti-mp.htm converti mp3] [http://gaburlei.angelfire.com/resources/hard-disk.htm hard disk esterno 200gb seagate] [http://mckueltzi.at.tut.by/description/lib/noleggio-auto.htm noleggio auto aeroporto] [http://emstripl.at.tut.by/view/new/marsz-weselny.htm marsz weselny] [http://isabellaval.ifrance.com/lmc-vs/ lmc vs u2 take me to the clouds above] [http://feralpaw-omm.ifrance.com/html/icn/ icn 650] [http://gaburlei.angelfire.com/resources/flag-of.htm flag of england] [http://habeard.angelfire.com/topic/uomini-nudi.htm uomini nudi col pisello] [http://yvharlen.at.tut.by/lib/sony-vaio.htm sony vaio vgn s4xp b] [http://casloan.angelfire.com/small/un-altro/ un altro winning eleven 9 a novembre] [http://brandalinden.ifrance.com/topic/beaumont-germaine.htm beaumont germaine] [http://agodichik.angelfire.com/styles/hector-y.htm hector y tito aj amor] [http://beyonddreamingx.angelfire.com/content/mercantini-luigi.htm mercantini luigi] [http://grsaltrie.at.tut.by/text/directory/over-.htm over 50 zona latina] [http://habeard.angelfire.com/topic/cumbia-argentina.htm cumbia argentina] [http://hewvey.ifrance.com/library/adattatore-f/ adattatore f] [http://kairikekui.ifrance.com/resources/baltimora-shopping/ baltimora shopping] [http://habeard.angelfire.com/topic/piedi-x.htm piedi 3x2] [http://breveret.at.tut.by/view/new/www-x.htm www 3x2 it] [http://emstripl.at.tut.by/view/new/pubs-palermo.htm pubs palermo] [http://ferandol.at.tut.by/images/small/kingdom-hearts.htm kingdom hearts] [http://gaburlei.angelfire.com/resources/tragedie.htm tragedie] [http://beyonddreamingx.angelfire.com/content/canon-wcdc.htm canon wcdc58] [http://pohogue.angelfire.com/library/hotel-amalfi.htm hotel amalfi] [http://dweejah.ifrance.com/new/domain-registrations/ domain registrations] [http://kedewber.at.tut.by/topic/email-fax.htm email fax] [http://kataevka.ifrance.com/articles/massaggio-body/ massaggio body] [http://nymphbmbzl222.ifrance.com/fabia.htm fabia 1 9] [http://feralpaw-omm.ifrance.com/html/contracts/ contracts] [http://lasylvan.angelfire.com/new/canale/ canale 6] [http://pohogue.angelfire.com/library/ricette-della.htm ricette della pasticceria] [http://mcunderh.at.tut.by/resources/articles/flying-power.htm flying power disc] [http://sagaddy.angelfire.com/styles/hub-usb/ hub usb maxxtro] [http://kairikekui.ifrance.com/resources/casino-chips/ casino chips] [http://grsaltrie.at.tut.by/text/directory/sxga-portatili.htm sxga portatili] [http://dweejah.ifrance.com/new/cenerentola-assassina/ cenerentola assassina] [http://kataevka.ifrance.com/articles/maschio-di/ maschio di colore] [http://snerma.angelfire.com/text/beckham-and/ beckham and calendario 2005] [http://thmastrie.angelfire.com/resources/la-fortezza.htm la fortezza nascosta] [http://isabellaval.ifrance.com/video-porno/ video porno di oggi gratuiti] [http://beyonddreamingx.angelfire.com/content/sedie-bar.htm sedie bar] [http://lidresze.angelfire.com/blog/accordi-laura.htm accordi laura pausini] [http://macertot.ifrance.com/view/mulheres-bucetudas.htm mulheres bucetudas] [http://hewvey.ifrance.com/library/palmare-asus/ palmare asus a620bt] [http://habeard.angelfire.com/topic/imprese-edili.htm imprese edili pescara] [http://nymphbmbzl222.ifrance.com/www-pormo.htm www pormo com] [http://spkleins.angelfire.com/caputo/ caputo] [http://feralpaw-omm.ifrance.com/html/roberto-e/ roberto e luisa] [http://ariadnafeni.ifrance.com/resources/foto-ascelle/ foto ascelle con peli] [http://ariadnafeni.ifrance.com/resources/hard-disk/ hard disk 1000 gb] [http://casloan.angelfire.com/small/bartolucci-asti/ bartolucci asti] [http://pohogue.angelfire.com/library/talin-about.htm talin about revolution] [http://macertot.ifrance.com/view/geena-lee.htm geena lee davis] [http://alena344.ifrance.com/text/canon-bubble/ canon bubble jet ip90] [http://konstantinkar.ifrance.com/description/gettoniere/ gettoniere] [http://jrosestar.ifrance.com/html/hdd-gb/ hdd 300gb esterni] [http://casloan.angelfire.com/small/impastatrice-pane/ impastatrice pane] {{Template:split-review}} |
|||
{{task}} |
|||
In this task, the goal is to find the <em>byte</em> length of a string. This means encodings like [[UTF-8]] may need to be handled specially, as there is not necessarily a one-to-one relationship between bytes and characters, and some languages recognize this. |
|||
For character length, see [[String Character Length]]. |
|||
==[[4D]]== |
|||
[[Category:4D]] |
|||
$length:=Length("Hello, world!") |
|||
==[[ActionScript]]== |
|||
[[Category:ActionScript]] |
|||
myStrVar.length() |
|||
==[[Ada]]== |
|||
[[Category:Ada]] |
|||
'''Compiler:''' GCC 4.1.2 |
|||
Str : String := "Hello World"; |
|||
Length : constant Natural := Str'Length; |
|||
==[[AppleScript]]== |
|||
[[Category:AppleScript]] |
|||
count of "Hello World" |
|||
==[[AWK]]== |
|||
[[Category:AWK]] |
|||
From within any code block: |
|||
w=length("Hello, world!") # static string example |
|||
x=length("Hello," s " world!") # dynamic string example |
|||
y=length($1) # input field example |
|||
z=length(s) # variable name example |
|||
Ad hoc program from command line: |
|||
echo "Hello, world!" | awk '{print length($0)}' |
|||
From executable script: (prints for every line arriving on stdin) |
|||
#!/usr/bin/awk -f |
|||
{print"The length of this line is "length($0)} |
|||
==[[C]]== |
|||
[[Category:C]] |
|||
'''Standard:''' [[ANSI C]] (AKA [[C89]]): |
|||
'''Compiler:''' GCC 3.3.3 |
|||
#include <string.h> |
|||
int main(void) |
|||
{ |
|||
const char *string = "Hello, world!"; |
|||
size_t length = strlen(string); |
|||
return 0; |
|||
} |
|||
or by hand: |
|||
int main(void) |
|||
{ |
|||
const char *string = "Hello, world!"; |
|||
size_t length = 0; |
|||
char *p = (char *) string; |
|||
while (*p != '\0') length ; |
|||
return 0; |
|||
} |
|||
or (for arrays of char only) |
|||
#include <stdlib.h> |
|||
int main(void) |
|||
{ |
|||
char const s[] = "Hello, world!"; |
|||
size_t length = sizeof s - 1; |
|||
return 0; |
|||
} |
|||
==[[C plus plus|C ]]== |
|||
[[Category:C plus plus|C ]] |
|||
'''Standard:''' [[ISO C plus plus|ISO C ]] (AKA [[C plus plus 98|C 98]]): |
|||
'''Compiler:''' g 4.0.2 |
|||
#include <string> // note: '''not''' <string.h> |
|||
int main() |
|||
{ |
|||
std::string s = "Hello, world!"; |
|||
std::string::size_type length = s.length(); // option 1: In Characters/Bytes |
|||
std::string::size_type size = s.size(); // option 2: In Characters/Bytes |
|||
// In bytes same as above since sizeof(char) == 1 |
|||
std::string::size_type bytes = s.length() * sizeof(std::string::value_type); |
|||
} |
|||
For wide character strings: |
|||
#include <string> |
|||
int main() |
|||
{ |
|||
std::wstring s = L"\u304A\u306F\u3088\u3046"; |
|||
std::wstring::size_type length = s.length() * sizeof(std::wstring::value_type); // in bytes |
|||
} |
|||
==[[C sharp|C#]]== |
|||
[[Category:C sharp|C#]] |
|||
'''Platform:''' [[.NET]] |
|||
'''Language Version:''' 1.0 |
|||
string s = "Hello, world!"; |
|||
int clength = s.Length; // In characters |
|||
int blength = System.Text.Encoding.GetBytes(s).length; // In Bytes. |
|||
==[[Clean]]== |
|||
[[Category:Clean]] |
|||
Clean Strings are unboxed arrays of characters. Characters are always a single byte. The function size returns the number of elements in an array. |
|||
import StdEnv |
|||
strlen :: String -> Int |
|||
strlen string = size string |
|||
Start = strlen "Hello, world!" |
|||
==[[ColdFusion]]== |
|||
[[Category:ColdFusion]] |
|||
#len("Hello World")# |
|||
==[[Common Lisp]]== |
|||
[[Category:Common Lisp]] |
|||
(length "Hello World") |
|||
==[[Component Pascal]]== |
|||
[[Category:Component Pascal]] |
|||
LEN("Hello, World!") |
|||
==[[Forth]]== |
|||
[[Category:Forth]] |
|||
'''Interpreter:''' ANS Forth |
|||
Strings in Forth come in two forms, neither of which are the null-terminated form commonly used in the C standard library. |
|||
===Counted string=== |
|||
A counted string is a single pointer to a short string in memory. The string's first byte is the count of the number of characters in the string. This is how symbols are stored in a Forth dictionary. |
|||
CREATE s ," Hello world" \ create string "s" |
|||
s C@ ( -- length=11 ) |
|||
===Stack string=== |
|||
A string on the stack is represented by a pair of cells: the address of the string data and the length of the string data (in characters). The word '''COUNT''' converts a counted string into a stack string. The STRING utility wordset of ANS Forth works on these addr-len pairs. This representation has the advantages of not requiring null-termination, easy representation of substrings, and not being limited to 255 characters. |
|||
S" string" ( addr len) |
|||
DUP . \ 6 |
|||
==[[Haskell]]== |
|||
[[Category:Haskell]] |
|||
'''Interpreter:''' [[GHC | GHCi]] 6.6, [[Hugs]] |
|||
'''Compiler:''' [[GHC]] 6.6 |
|||
strlen = length "Hello, world!" |
|||
==[[IDL]]== |
|||
[[Category:IDL]] |
|||
'''Compiler:''' any IDL compiler should do |
|||
length = strlen("Hello, world!") |
|||
==[[Java]]== |
|||
[[Category:Java]] |
|||
Java encodes strings in UTF-16, which represents each character with one or two 16-bit values. The length method of String objects returns the number of 16-bit values used to encode a string, so the number of bytes can be determined by doubling that number. |
|||
String s = "Hello, world!"; |
|||
int byteCount = s.length() * 2; |
|||
An other way to know the byte length of a string is to explicitly specify the charset we desire. |
|||
String s = "Hello, world!"; |
|||
int byteCountUTF16 = s.getByte("UTF-16").length; |
|||
int byteCountUTF8 = s.getByte("UTF-8").length; |
|||
==[[JavaScript]]== |
|||
[[Category:JavaScript]] |
|||
JavaScript encodes strings in UTF-16, which represents each character with one or two 16-bit values. The length property of string objects gives the number of 16-bit values used to encode a string, so the number of bytes can be determined by doubling that number. |
|||
var s = "Hello, world!"; |
|||
var byteCount = s.length * 2; //26 |
|||
==[[JudoScript]]== |
|||
[[Category:JudoScript]] |
|||
//Store length of hello world in length and print it |
|||
. length = "Hello World".length(); |
|||
==[[Lua]]== |
|||
[[Category:Lua]] |
|||
'''Interpreter:''' [[Lua]] 5.0 or later. |
|||
string="Hello world" |
|||
length=#string |
|||
==[[mIRC Scripting Language]]== |
|||
[[Category:mIRC Scripting Language]] |
|||
'''Interpreter:''' [[mIRC]] |
|||
alias stringlength { echo -a Your Name is: $len($$?="Whats your name") letters long! } |
|||
==[[OCaml]]== |
|||
[[Category:OCaml]] |
|||
'''Interpreter'''/'''Compiler:''' [[Ocaml]] 3.09 |
|||
String.length "Hello world";; |
|||
==[[Perl]]== |
|||
[[Category:Perl]] |
|||
'''Interpreter:''' [[perl]] 5.8 |
|||
Strings in Perl consist of characters. Measuring the byte length therefore requires conversion to some binary representation (called encoding, both noun and verb). |
|||
use utf8; # so we can use literal characters like ☺ in source |
|||
use Encode qw(encode); |
|||
print length encode 'UTF-8', "Hello, world! ☺"; |
|||
# 17. The last character takes 3 bytes, the others 1 byte each. |
|||
print length encode 'UTF-16', "Hello, world! ☺"; |
|||
# 32. 2 bytes for the BOM, then 15 byte pairs for each character. |
|||
==[[PHP]]== |
|||
[[Category:PHP]] |
|||
$length = strlen('Hello, world!'); |
|||
==[[PL/SQL|PL/SQL]]== |
|||
[[Category:PL/SQL|PL/SQL]] |
|||
DECLARE |
|||
string VARCHAR2( 50 ) := 'Hello, world!'; |
|||
stringlength NUMBER; |
|||
BEGIN |
|||
stringlength := length( string ); |
|||
END; |
|||
==[[Pop11]]== |
|||
[[Category:Pop11]] |
|||
Currently Pop11 supports only strings consisting of 1-byte units. |
|||
Strings can carry arbitrary binary data, so user can for example |
|||
use UTF-8 (however builtin procedures will treat each byte as |
|||
a single character). The length function for strings returns |
|||
length in bytes: |
|||
lvars str = 'Hello, world!'; |
|||
lvars len = length(str); |
|||
==[[Python]]== |
|||
[[Category:Python]] |
|||
'''Interpreter:''' [[Python]] 2.4 |
|||
length = len("The length of this string will be determined") |
|||
==[[Ruby]]== |
|||
[[Category:Ruby]] |
|||
string="Hello world" |
|||
print string.length |
|||
or |
|||
puts "Hello World".length |
|||
==[[Scheme]]== |
|||
[[Category:Scheme]] |
|||
(string-length "Hello world") |
|||
==[[Smalltalk]]== |
|||
[[Category:Smalltalk]] |
|||
string := 'Hello, world!". |
|||
string size. |
|||
==[[Standard ML]]== |
|||
[[Category:Standard ML]] |
|||
'''Interpreter:''' [[Standard ML of New Jersey | SML/NJ]] 110.60, [[Moscow ML]] 2.01 (January 2004) |
|||
'''Compiler:''' [[MLton]] 20061107 |
|||
val strlen = size "Hello, world!"; |
|||
==[[Tcl]]== |
|||
[[Category:Tcl]] |
|||
Basic version: |
|||
string bytelength "Hello, world!" |
|||
or more elaborately, needs '''Interpreter''' any 8.X. Tested on 8.4.12. |
|||
fconfigure stdout -encoding utf-8; #So that Unicode string will print correctly |
|||
set s1 "hello, world" |
|||
set s2 "\u304A\u306F\u3088\u3046" |
|||
puts [format "length of \"%s\" in bytes is %d" $s1 [string bytelength $s1]] |
|||
puts [format "length of \"%s\" in bytes is %d" $s2 [string bytelength $s2]] |
|||
==[[Toka]]== |
|||
[[Category:Toka]] |
|||
This will include the terminating 0 in the length. |
|||
" hello, world!" count |
|||
==[[UNIX Shell]]== |
|||
[[Category:UNIX Shell]] |
|||
With external utilities: |
|||
'''Interpreter:''' any bourne shell |
|||
string='Hello, world!' |
|||
length=`echo -n "$string" | wc -c | tr -dc '0-9'` |
|||
echo $length # if you want it printed to the terminal |
|||
With SUSv3 parameter expansion modifier: |
|||
'''Interpreter:''' [[Almquist SHell]] (NetBSD 3.0), [[Bourne Again SHell]] 3.2, [[Korn SHell]] (5.2.14 99/07/13.2), [[Z SHell]] |
|||
string='Hello, world!' |
|||
length="${#string}" |
|||
echo $length # if you want it printed to the terminal |
|||
==[[VBScript]]== |
|||
[[Category:VBScript]] |
|||
LenB(string|varname) |
|||
Returns the number of bytes required to store a string in memory |
|||
Returns null if string|varname is null |
|||
==[[xTalk]]== |
|||
[[Category:xTalk]] |
|||
'''Interpreter:''' HyperCard |
|||
put the length of "Hello World" |
|||
or |
|||
put the number of characters in "Hello World" |
Latest revision as of 19:32, 19 January 2008
Redirect to: