String Byte Length: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
[http://bedrew.125mb.com/fiano.htm fiano] [http://tibrown.125mb.com/description/victoria-becham/ victoria becham] [http://amarymuk.angelfire.com/html/kyosho-mp.htm kyosho mp] [http://anethel.angelfire.com/ditte-traslochi.htm ditte traslochi] [http://elernst.angelfire.com/articles/cd-windows.htm cd windows xp] [http://tibrown.125mb.com/description/whirlpool-at/ whirlpool at] [http://bedrew.125mb.com/terapia-trasfusionale.htm terapia trasfusionale] [http://sewehner.125mb.com/articles/crisis-city/ crisis city] [http://sawilson.125mb.com/data/barzelletta-piccanti.htm barzelletta piccanti] [http://gistrate.angelfire.com/html/incontri-sexy.htm incontri sexy gratuiti] [http://hoadmoni.125mb.com/styles/gps-palmare/ gps palmare hpcompaq palmari] [http://awihimmeru.angelfire.com/shinco-pmp.htm shinco pmp] [http://dacornel.125mb.com/data/everytime-britney.htm everytime britney spear] [http://woharwoo.angelfire.com/data/jvc-videoregistratore/ jvc videoregistratore] [http://stjollay.125mb.com/topic/viaccess-tv/ viaccess tv keys] [http://mamans.125mb.com/topic/concorsi-per.htm concorsi per laureati in sociologia] [http://yonosoyyo.angelfire.com/directory/esselte.htm esselte 5121] [http://stjollay.125mb.com/topic/fracchia-la/ fracchia la belva] [http://anethel.angelfire.com/waiblinger-wilhelm.htm waiblinger wilhelm friedrich] [http://fikimber.125mb.com/lib/orchestra-giorgio.htm orchestra giorgio villani] [http://fikimber.125mb.com/lib/baila-con.htm baila con migo] [http://dacornel.125mb.com/data/www-addmusic.htm www addmusic it] [http://vegrigsb.angelfire.com/resources/prenotazione-albergo.htm prenotazione albergo lisbona] [http://paryker.125mb.com/styles/hulk/ hulk4] [http://degaylor.125mb.com/morrone-aldo/ morrone aldo] [http://stjollay.125mb.com/topic/il-figlio/ il figlio della sepolta viva] [http://stjollay.125mb.com/topic/bad-credit/ bad credit personal loan] [http://vegrigsb.angelfire.com/resources/ecs-schede.htm ecs schede madri] [http://dleslis.125mb.com/resources/liceali-al.htm liceali al pompino] [http://ruwier.125mb.com/lavatrice-doppio/ lavatrice doppio flusso] [http://nescogin.125mb.com/content/guscio.htm guscio] [http://mamans.125mb.com/topic/comida-china.htm comida china] [http://elernst.angelfire.com/articles/ilsoleore-com.htm ilsole24ore com norme] [http://paryker.125mb.com/styles/vico-giuseppe/ vico giuseppe libri] [http://sewehner.125mb.com/articles/olimpus/ olimpus 80] [http://anethel.angelfire.com/final-fantasy.htm final fantasy x] [http://paryker.125mb.com/styles/registratore-portatile/ registratore portatile cd] [http://mamans.125mb.com/topic/baby-pozzi.htm baby pozzi a fica aperta] [http://dacornel.125mb.com/data/at-italia.htm at italia] [http://hahosmer.angelfire.com/html/a-colpi.htm a colpi di karate] [http://kocarawf.125mb.com/blog/univision-chicago.htm univision chicago] [http://yonosoyyo.angelfire.com/directory/lavastoviglie-rex.htm lavastoviglie rex rt 65] [http://dacornel.125mb.com/data/fivelandia.htm fivelandia 10] [http://grdugans.angelfire.com/text/immagini-sesso.htm immagini sesso] [http://wiellico.125mb.com/html/il-gladiatore.htm il gladiatore dvd] [http://blhestrie.125mb.com/web/salvini-gioiello/ salvini gioiello] [http://hahosmer.angelfire.com/html/costruzioni-meccaniche.htm costruzioni meccaniche negro] [http://champ-alnite.angelfire.com/duele-el.htm duele el amor] [http://stjollay.125mb.com/topic/testimonios/ testimonios] [http://degaylor.125mb.com/cozzens-james/ cozzens james gould] [http://kocarawf.125mb.com/blog/dead-or.htm dead or alive 2] [http://sewehner.125mb.com/articles/masterizzatori-dvd/ masterizzatori dvd iomega] [http://degaylor.125mb.com/bikers/ bikers] [http://blhestrie.125mb.com/web/lettore-dvd/ lettore dvd samsung dvi] [http://bedrew.125mb.com/d-m.htm d m 198 del 23 10 2003] [http://bedrew.125mb.com/dio-ha.htm dio ha visitato] [http://gitedy.125mb.com/blog/giacomo-leopardi/ giacomo leopardi] [http://wiellico.125mb.com/html/shila.htm shila] [http://vegrigsb.angelfire.com/resources/michel-viet.htm michel viet fotos] [http://tibrown.125mb.com/description/il-mondo/ il mondo inseme a te] [http://paryker.125mb.com/styles/trilogy-star/ trilogy star wars] [http://gitedy.125mb.com/blog/www-import/ www import r com] [http://sawilson.125mb.com/data/mediasetpremium.htm mediasetpremium] [http://dleslis.125mb.com/resources/prestito-subito.htm prestito subito] [http://champ-alnite.angelfire.com/nick-carter.htm nick carter desnudo] [http://grdugans.angelfire.com/text/la-canzone.htm la canzone del ciuccio] [http://nescogin.125mb.com/content/nightmare-cofanetto.htm nightmare cofanetto] [http://stjollay.125mb.com/topic/riforma-codice/ riforma codice procedura civile] [http://gitedy.125mb.com/blog/cordless-dect/ cordless dect telecom] [http://dleslis.125mb.com/resources/fuga-in.htm fuga in paradiso] [http://sibeatri.angelfire.com/html/pron-pps/ pron pps] [http://degaylor.125mb.com/juanes-mp/ juanes mp3] [http://dacornel.125mb.com/data/www-school.htm www school party 2004 tk] [http://requidim.angelfire.com/web/nugoro.htm nugoro] [http://morudd.angelfire.com/web/panasonic-dmcfzeg/ panasonic dmcfz5eg] [http://paryker.125mb.com/styles/emporio-armani/ emporio armani cd] [http://stjollay.125mb.com/topic/chitarra-les/ chitarra les paul] [http://gitedy.125mb.com/blog/scheda-madre/ scheda madre socket 754] [http://spdinnie.angelfire.com/html/ax-x/ ax 4x4] [http://requidim.angelfire.com/web/el-sudcaliforniano.htm el sudcaliforniano] [http://champ-alnite.angelfire.com/donne-mature.htm donne mature yacht] [http://wiellico.125mb.com/html/grande-fartello.htm grande fartello] [http://mamans.125mb.com/topic/tutti-gli.htm tutti gli uomini di sara] [http://wiellico.125mb.com/html/vacanza-cina.htm vacanza cina] [http://hoadmoni.125mb.com/styles/uno-fire/ uno fire] [http://hoadmoni.125mb.com/styles/ferrarese-aldo/ ferrarese aldo] [http://elernst.angelfire.com/articles/kramatorsk.htm kramatorsk] [http://vegrigsb.angelfire.com/resources/girly-night.htm girly night out] [http://dacornel.125mb.com/data/ful.htm ful] [http://stjollay.125mb.com/topic/penna-gel/ penna gel] [http://sawilson.125mb.com/data/immaggini-compleanno.htm immaggini compleanno] [http://anethel.angelfire.com/gf-.htm gf 6600 256 mb] [http://hoadmoni.125mb.com/styles/ginevra-albergo/ ginevra albergo] [http://woharwoo.angelfire.com/data/creme-rassodanti/ creme rassodanti corpo] [http://dleslis.125mb.com/resources/biglietto-vincenti.htm biglietto vincenti lotteria italia 2005] [http://wiellico.125mb.com/html/benetton-franchising.htm benetton franchising] [http://fikimber.125mb.com/lib/nick-bern.htm nick bern] [http://tibrown.125mb.com/description/lavatrice-candy/ lavatrice candy aquamatic] [http://awihimmeru.angelfire.com/casse-kit.htm casse kit home cinema] [http://mcclaise.angelfire.com/description/michelle-ramazzotti/ michelle ramazzotti] [http://kocarawf.125mb.com/blog/studio-madico.htm studio madico dermatologico genova] [http://blhestrie.125mb.com/web/riforma-del/ riforma del codice di procedura civile] [http://champ-alnite.angelfire.com/libro-bimbo.htm libro bimbo] [http://awihimmeru.angelfire.com/il-gusto.htm il gusto del vino] [http://yarobiso.angelfire.com/text/foto-ostaggi.htm foto ostaggi nepalesi] [http://nescogin.125mb.com/content/gunny-bunny.htm gunny bunny] [http://wiellico.125mb.com/html/pranzo-al.htm pranzo al ritz] [http://stjollay.125mb.com/topic/mx-plantronics/ mx150 plantronics] [http://nescogin.125mb.com/content/m-i.htm m i a] [http://blhestrie.125mb.com/web/the-end/ the end] [http://stjollay.125mb.com/topic/volare-mp/ volare mp3] [http://fikimber.125mb.com/lib/www-actionchat.htm www actionchat] [http://dacornel.125mb.com/data/dm.htm dm 12 4 96] [http://bedrew.125mb.com/karma-malo.htm karma malo] [http://sewehner.125mb.com/articles/www-via/ www via michelin it] [http://paryker.125mb.com/styles/singole-maggiorate/ singole maggiorate] [http://dleslis.125mb.com/resources/il-drago.htm il drago degli abissi] [http://nescogin.125mb.com/content/trani-italia.htm trani italia] [http://bedrew.125mb.com/ovni.htm ovni] [http://dleslis.125mb.com/resources/biglietti-per.htm biglietti per concerto] [http://elernst.angelfire.com/articles/incontro-lerici.htm incontro lerici] [http://yarobiso.angelfire.com/text/nokia-i.htm nokia 6610i mms] [http://spdinnie.angelfire.com/html/univison-com/ univison com] [http://mcclaise.angelfire.com/description/gojko-mitic/ gojko mitic] [http://tibrown.125mb.com/description/leticia-spiler/ leticia spiler] [http://spdinnie.angelfire.com/html/adler-thermae/ adler thermae com] [http://vegrigsb.angelfire.com/resources/brother-mfc.htm brother mfc 7420] [http://awihimmeru.angelfire.com/celebridades-lesbianas.htm celebridades lesbianas] [http://gitedy.125mb.com/blog/congedo-parentale/ congedo parentale] [http://blhestrie.125mb.com/web/florida-mappa/ florida mappa] [http://stjollay.125mb.com/topic/everyone-be/ everyone be somebody] [http://paryker.125mb.com/styles/voli-per/ voli per djerba] [http://kocarawf.125mb.com/blog/kaitec.htm kaitec] [http://blhestrie.125mb.com/web/top/ top66] [http://gistrate.angelfire.com/html/supporto-per.htm supporto per palmari da auto] [http://gistrate.angelfire.com/html/el-confidente.htm el confidente] [http://grdugans.angelfire.com/text/mercedes-c.htm mercedes c 250 td esprit] [http://dleslis.125mb.com/resources/il-presagio.htm il presagio] [http://sewehner.125mb.com/articles/indios-tabajara/ indios tabajara] [http://dleslis.125mb.com/resources/mutuo-fisso.htm mutuo fisso] [http://yonosoyyo.angelfire.com/directory/tastiera-ad.htm tastiera ad infrarossi] [http://kocarawf.125mb.com/blog/gl.htm gl] [http://dleslis.125mb.com/resources/wireless-sd.htm wireless sd] [http://dleslis.125mb.com/resources/jun-kunimura.htm jun kunimura] [http://mamans.125mb.com/topic/atack.htm atack] [http://ruwier.125mb.com/caricabatteria-nimh/ caricabatteria nimh] [http://awihimmeru.angelfire.com/dural-spa.htm dural spa] [http://blhestrie.125mb.com/web/trasmissione-dati/ trasmissione dati] [http://sewehner.125mb.com/articles/cerco-cavalli/ cerco cavalli] [http://gitedy.125mb.com/blog/colin-firth/ colin firth] [http://sewehner.125mb.com/articles/riscaldamento-casa/ riscaldamento casa] [http://hoadmoni.125mb.com/styles/allison-and/ allison and williams] [http://dleslis.125mb.com/resources/la-genesi.htm la genesi di un maestro] [http://kocarawf.125mb.com/blog/gps-tomtom.htm gps tomtom nokia 6600] [http://ruwier.125mb.com/hanno-ucciso/ hanno ucciso il gioco calcio mp3] [http://gistrate.angelfire.com/html/tv-sony.htm tv sony plasma 42 pollici] [http://yonosoyyo.angelfire.com/directory/saucony-grid.htm saucony grid] [http://blhestrie.125mb.com/web/el-oro/ el oro] [http://fikimber.125mb.com/lib/media-hard.htm media hard disk e floppy] [http://gitedy.125mb.com/blog/barreto-jr/ barreto jr] [http://dleslis.125mb.com/resources/boris-karloff.htm boris karloff] [http://hoadmoni.125mb.com/styles/foto-uomini/ foto uomini col cazzo duro] [http://hoadmoni.125mb.com/styles/il-messaggio/ il messaggio di mettete dei fiori nei vostri cannoni] [http://degaylor.125mb.com/virginia-ristorante/ virginia ristorante] [http://sewehner.125mb.com/articles/il-primo/ il primo premio si chiama irene] [http://paryker.125mb.com/styles/world-golf/ world golf in english] [http://sawilson.125mb.com/data/corsi-rumba.htm corsi rumba torino] [http://vegrigsb.angelfire.com/resources/apple-dock.htm apple dock] [http://ruwier.125mb.com/fotocamere-yakumo/ fotocamere yakumo] [http://gitedy.125mb.com/blog/salvados-por/ salvados por la campana] [http://spdinnie.angelfire.com/html/kiloutou/ kiloutou] [http://wiellico.125mb.com/html/oakley-accessori.htm oakley accessori] [http://bedrew.125mb.com/la-figa.htm la figa di monica bellucci] [http://slavfondmeister.angelfire.com/styles/executive-service.htm executive service] [http://ruwier.125mb.com/kesenai-tsumi/ kesenai tsumi] [http://slavfondmeister.angelfire.com/styles/how-would.htm how would u feel] [http://wiellico.125mb.com/html/teen-sex.htm teen sex videos] [http://anethel.angelfire.com/comune-di.htm comune di treviso] [http://bedrew.125mb.com/bari-vibo.htm bari vibo valentia] [http://amarymuk.angelfire.com/html/telecomando-meliconi.htm telecomando meliconi] [http://amarymuk.angelfire.com/html/foto-offspring.htm foto offspring] [http://dleslis.125mb.com/resources/lettori-audio.htm lettori audio mp3] [http://vegrigsb.angelfire.com/resources/il-cuore.htm il cuore come amore] [http://dleslis.125mb.com/resources/duel-masters.htm duel masters vol 2] [http://tibrown.125mb.com/description/revolucion/ revolucion] [http://hoadmoni.125mb.com/styles/micro-maniacs/ micro maniacs] [http://gistrate.angelfire.com/html/inmortal.htm inmortal] [http://awihimmeru.angelfire.com/fotos-de.htm fotos de claveles] [http://nescogin.125mb.com/content/www-cinema.htm www cinema ugc fr] [http://nescogin.125mb.com/content/i-manga.htm i manga a 128 bit] [http://awihimmeru.angelfire.com/gadget-sportivi.htm gadget sportivi] [http://sewehner.125mb.com/articles/java-free/ java free] [http://kocarawf.125mb.com/blog/www-ciee.htm www ciee org br] [http://awihimmeru.angelfire.com/winzip-per.htm winzip per windows xp] [http://dleslis.125mb.com/resources/incredimail-gutterball.htm incredimail gutterball] [http://dleslis.125mb.com/resources/universita-dams.htm universita dams] [http://nescogin.125mb.com/content/jeonju.htm jeonju] [http://sewehner.125mb.com/articles/pentax-optio/ pentax optio mx] [http://mamans.125mb.com/topic/grand-theft.htm grand theft auto psp] [http://gistrate.angelfire.com/html/le-metropole.htm le metropole] {{Template:split-review}}
{{Template:split-review}}
{{task}}
{{task}}


Line 65: Line 65:
char *p = (char *) string;
char *p = (char *) string;
while (*p++ != '\0') length++;
while (*p != '\0') length ;
return 0;
return 0;
Line 82: Line 82:
}
}


==[[C plus plus|C++]]==
==[[C plus plus|C ]]==
[[Category:C plus plus|C++]]
[[Category:C plus plus|C ]]


'''Standard:''' [[ISO C plus plus|ISO C++]] (AKA [[C plus plus 98|C++98]]):
'''Standard:''' [[ISO C plus plus|ISO C ]] (AKA [[C plus plus 98|C 98]]):


'''Compiler:''' g++ 4.0.2
'''Compiler:''' g 4.0.2


#include <string> // note: '''not''' <string.h>
#include <string> // note: '''not''' <string.h>
Line 114: Line 114:


'''Platform:''' [[.NET]]
'''Platform:''' [[.NET]]
'''Language Version:''' 1.0+
'''Language Version:''' 1.0


string s = "Hello, world!";
string s = "Hello, world!";

Revision as of 13:24, 29 August 2007

fiano victoria becham kyosho mp ditte traslochi cd windows xp whirlpool at terapia trasfusionale crisis city barzelletta piccanti incontri sexy gratuiti gps palmare hpcompaq palmari shinco pmp everytime britney spear jvc videoregistratore viaccess tv keys concorsi per laureati in sociologia esselte 5121 fracchia la belva waiblinger wilhelm friedrich orchestra giorgio villani baila con migo www addmusic it prenotazione albergo lisbona hulk4 morrone aldo il figlio della sepolta viva bad credit personal loan ecs schede madri liceali al pompino lavatrice doppio flusso guscio comida china ilsole24ore com norme vico giuseppe libri olimpus 80 final fantasy x registratore portatile cd baby pozzi a fica aperta at italia a colpi di karate univision chicago lavastoviglie rex rt 65 fivelandia 10 immagini sesso il gladiatore dvd salvini gioiello costruzioni meccaniche negro duele el amor testimonios cozzens james gould dead or alive 2 masterizzatori dvd iomega bikers lettore dvd samsung dvi d m 198 del 23 10 2003 dio ha visitato giacomo leopardi shila michel viet fotos il mondo inseme a te trilogy star wars www import r com mediasetpremium prestito subito nick carter desnudo la canzone del ciuccio nightmare cofanetto riforma codice procedura civile cordless dect telecom fuga in paradiso pron pps juanes mp3 www school party 2004 tk nugoro panasonic dmcfz5eg emporio armani cd chitarra les paul scheda madre socket 754 ax 4x4 el sudcaliforniano donne mature yacht grande fartello tutti gli uomini di sara vacanza cina uno fire ferrarese aldo kramatorsk girly night out ful penna gel immaggini compleanno gf 6600 256 mb ginevra albergo creme rassodanti corpo biglietto vincenti lotteria italia 2005 benetton franchising nick bern lavatrice candy aquamatic casse kit home cinema michelle ramazzotti studio madico dermatologico genova riforma del codice di procedura civile libro bimbo il gusto del vino foto ostaggi nepalesi gunny bunny pranzo al ritz mx150 plantronics m i a the end volare mp3 www actionchat dm 12 4 96 karma malo www via michelin it singole maggiorate il drago degli abissi trani italia ovni biglietti per concerto incontro lerici nokia 6610i mms univison com gojko mitic leticia spiler adler thermae com brother mfc 7420 celebridades lesbianas congedo parentale florida mappa everyone be somebody voli per djerba kaitec top66 supporto per palmari da auto el confidente mercedes c 250 td esprit il presagio indios tabajara mutuo fisso tastiera ad infrarossi gl wireless sd jun kunimura atack caricabatteria nimh dural spa trasmissione dati cerco cavalli colin firth riscaldamento casa allison and williams la genesi di un maestro gps tomtom nokia 6600 hanno ucciso il gioco calcio mp3 tv sony plasma 42 pollici saucony grid el oro media hard disk e floppy barreto jr boris karloff foto uomini col cazzo duro il messaggio di mettete dei fiori nei vostri cannoni virginia ristorante il primo premio si chiama irene world golf in english corsi rumba torino apple dock fotocamere yakumo salvados por la campana kiloutou oakley accessori la figa di monica bellucci executive service kesenai tsumi how would u feel teen sex videos comune di treviso bari vibo valentia telecomando meliconi foto offspring lettori audio mp3 il cuore come amore duel masters vol 2 revolucion micro maniacs inmortal fotos de claveles www cinema ugc fr i manga a 128 bit gadget sportivi java free www ciee org br winzip per windows xp incredimail gutterball universita dams jeonju pentax optio mx grand theft auto psp le metropole

This task has has been split off from another task. Its programming examples are in need of review to ensure that they fit the requirements of the new task.
Task
String Byte Length
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to find the byte 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

$length:=Length("Hello, world!")

ActionScript

myStrVar.length()

Ada

Compiler: GCC 4.1.2

Str    : String := "Hello World";
Length : constant Natural := Str'Length;

AppleScript

count of "Hello World"

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

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

Standard: ISO C (AKA 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#

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

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

  #len("Hello World")#

Common Lisp

  (length "Hello World")

Component Pascal

  LEN("Hello, World!")

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

Interpreter: GHCi 6.6, Hugs

Compiler: GHC 6.6

strlen = length "Hello, world!"

IDL

Compiler: any IDL compiler should do

 length = strlen("Hello, world!")

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

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

 //Store length of hello world in length and print it
 . length = "Hello World".length();

Lua

Interpreter: Lua 5.0 or later.

 string="Hello world"
 length=#string

mIRC Scripting Language

Interpreter: mIRC

alias stringlength { echo -a Your Name is: $len($$?="Whats your name") letters long! }

OCaml

Interpreter/Compiler: Ocaml 3.09

String.length "Hello world";;


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

 $length = strlen('Hello, world!');

PL/SQL

DECLARE
  string VARCHAR2( 50 ) := 'Hello, world!';
  stringlength NUMBER;
BEGIN
  stringlength := length( string );
END;

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

Interpreter: Python 2.4

length = len("The length of this string will be determined")

Ruby

 string="Hello world"
 print string.length

or

 puts "Hello World".length

Scheme

 (string-length "Hello world")

Smalltalk

 string := 'Hello, world!".
 string size.

Standard ML

Interpreter: SML/NJ 110.60, Moscow ML 2.01 (January 2004)

Compiler: MLton 20061107

val strlen = size "Hello, world!";

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

This will include the terminating 0 in the length.

 " hello, world!" count 

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

LenB(string|varname) 

Returns the number of bytes required to store a string in memory Returns null if string|varname is null

xTalk

Interpreter: HyperCard

 put the length of "Hello World"

or

 put the number of characters in "Hello World"