String Character Length: Difference between revisions
mNo edit summary |
Undo revision 6989 by Special:Contributions/SdqZt5 (User talk:SdqZt5) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://lovereceier.ifrance.com/lib/french-kiss.htm french kiss dvd] [http://yeeden.ifrance.com/img/styles/libri-di/ libri di cucina] [http://highbulp.ifrance.com/images/small/mixer-audiovideo/ mixer audio-video] [http://singaporepets.ifrance.com/library/html/amd-duron.htm amd duron ventole] [http://demurediablo.ifrance.com/library/html/www-foto/ www foto gratis] [http://highbulp.ifrance.com/images/small/www-nokia/ www nokia] [http://tizolaa.ifrance.com/web/immagini-sulla/ immagini sulla turchia] [http://lordsander.ifrance.com/topic/picchiaduro-psp.htm picchiaduro psp videogiochi] [http://broadwaylili.ifrance.com/content/view/gonzaga-mantova.htm gonzaga (mantova)] [http://lordsander.ifrance.com/topic/riassunti-verga.htm riassunti verga] [http://tat-ooin.ifrance.com/resources/articles/bellenden-william.htm bellenden, william] [http://toofarfrommaybe.ifrance.com/view/new/ufficio-vercelli/ ufficio vercelli] [http://girlwho-is.ifrance.com/img/styles/scarica-suonerie.htm scarica suonerie ommitel it] [http://girlwho-is.ifrance.com/img/styles/ospedale-rizzoli.htm ospedale rizzoli bologna] [http://noxuhax.ifrance.com/content/view/googfle.htm googfle] [http://voltia.ifrance.com/content/view/fidanzata-amico.htm fidanzata amico] [http://broadwaylili.ifrance.com/content/view/grosseto-trans.htm grosseto trans] [http://deviantrus.ifrance.com/topic/poliziotto-solitudine/ poliziotto, solitudine e rabbia] [http://toofarfrommaybe.ifrance.com/view/new/barbie-magico/ barbie magico salone di bellezza] [http://kalian42.ifrance.com/text/directory/ad-ware/ ad ware] [http://igaros.ifrance.com/lib/how-deep/ how deep is] [http://jdawsona.ifrance.com/data/yakumo-x/ yakumo 67x] [http://lordsander.ifrance.com/topic/aeg.htm aeg 48380] [http://tizolaa.ifrance.com/web/domenica-bestiale/ domenica bestiale di fabio concato] [http://noxuhax.ifrance.com/content/view/driver-for.htm driver for motorola v220] [http://kalian42.ifrance.com/text/directory/classrooms/ classrooms] [http://singaporepets.ifrance.com/library/html/head-radical.htm head radical liquidmetal] [http://deviantrus.ifrance.com/topic/melissa/ melissa 18] [http://broadwaylili.ifrance.com/content/view/dlt-backup.htm dlt back-up e accessori] [http://tizolaa.ifrance.com/web/w-w/ w w w cameracafe it] [http://girlwho-is.ifrance.com/img/styles/microsoft-digital.htm microsoft digital media pro keyboard] [http://tizolaa.ifrance.com/web/roba-che/ roba che scotta] [http://deviantrus.ifrance.com/topic/la-perla/ la perla store] [http://girlwho-is.ifrance.com/img/styles/marmisti.htm marmisti] [http://voltia.ifrance.com/content/view/bruce-springsteen.htm bruce springsteen. video anthology 1978-88] [http://kalian42.ifrance.com/text/directory/pentium-/ pentium 4 socket 478 3 2 ghz] [http://noxuhax.ifrance.com/content/view/torino-roma.htm torino roma] [http://lordsander.ifrance.com/topic/ragazza.htm ragazza] [http://kalian42.ifrance.com/text/directory/vita-segreta/ vita segreta di una diciottenne] [http://highbulp.ifrance.com/images/small/football-manager/ football manager] [http://noxuhax.ifrance.com/content/view/gli-ultimi.htm gli ultimi giorni] [http://tizolaa.ifrance.com/web/max-speziali/ max speziali] [http://speedofsoun.ifrance.com/text/directory/code-keys.htm code keys viaccess seca nagravision] [http://tat-ooin.ifrance.com/resources/articles/real-book.htm real book] [http://demurediablo.ifrance.com/library/html/nuova-officine/ nuova officine tessili] [http://jdawsona.ifrance.com/data/san-sperate/ san sperate] [http://yeeden.ifrance.com/img/styles/hp-scanjet/ hp scanjet 5530c usb] [http://girlwho-is.ifrance.com/img/styles/livorno.htm livorno] [http://toofarfrommaybe.ifrance.com/view/new/il-simbolo/ il simbolo del sesso] [http://kalian42.ifrance.com/text/directory/fashion-show/ fashion show clip] [http://jdawsona.ifrance.com/data/printer-dock/ printer dock 6000] [http://tizolaa.ifrance.com/web/bon-jovi/ bon jovi in these arms] [http://lovereceier.ifrance.com/lib/battle.htm battle2] [http://jdawsona.ifrance.com/data/antenna-dsail/ antenna d-sail] [http://highbulp.ifrance.com/images/small/marica-yo/ marica yo marica tu marica quien marica] [http://tizolaa.ifrance.com/web/libreria-ibis/ libreria ibis] [http://igaros.ifrance.com/lib/www-biancaluna/ www biancaluna com] [http://lordsander.ifrance.com/topic/crociera-nilo.htm crociera nilo vacanze] [http://tat-ooin.ifrance.com/resources/articles/ram-cisco.htm ram cisco 1700] [http://tat-ooin.ifrance.com/resources/articles/volo-basso.htm volo basso costo] [http://singaporepets.ifrance.com/library/html/mariah-carey.htm mariah carey porno foto] [http://lordsander.ifrance.com/topic/mina-don.htm mina don t] [http://deviantrus.ifrance.com/topic/tim-sms/ tim sms gratis] [http://tat-ooin.ifrance.com/resources/articles/daniela-mercury.htm daniela mercury] [http://tizolaa.ifrance.com/web/foppapedretti-mary/ foppapedretti mary naturale] [http://speedofsoun.ifrance.com/text/directory/golf.htm golf 3.2] [http://kalian42.ifrance.com/text/directory/luci-a/ luci a sa siro] [http://akmokanzen.ifrance.com/images/small/moto-recapiti.htm moto recapiti] [http://oiyaoi.ifrance.com/images/small/liquido-campeggio/ liquido campeggio] [http://kalian42.ifrance.com/text/directory/come-si/ come si installa] [http://broadwaylili.ifrance.com/content/view/dezful.htm dezful] [http://tizolaa.ifrance.com/web/sborra-negli/ sborra negli slip] [http://jdawsona.ifrance.com/data/oubritenga/ oubritenga] [http://highbulp.ifrance.com/images/small/sito-ufficiale/ sito ufficiale italiano per wolverine] [http://yeeden.ifrance.com/img/styles/cassino/ cassino] [http://voltia.ifrance.com/content/view/ram-full.htm ram full brand] [http://akmokanzen.ifrance.com/images/small/michael-j.htm michael j fox] [http://girlwho-is.ifrance.com/img/styles/enciende-mi.htm enciende mi fuego] [http://lordsander.ifrance.com/topic/balla-diy.htm balla diy] [http://tizolaa.ifrance.com/web/surf-casting/ surf casting] [http://tat-ooin.ifrance.com/resources/articles/grande-punto.htm grande punto 1 3] [http://tizolaa.ifrance.com/web/le-fate/ le fate dei fiori] [http://broadwaylili.ifrance.com/content/view/la-stanza.htm la stanza del fliglio] [http://voltia.ifrance.com/content/view/monitor-plasma.htm monitor plasma hitachi] [http://noxuhax.ifrance.com/content/view/olivetti-ofx.htm olivetti ofx] [http://yeeden.ifrance.com/img/styles/courtney/ courtney] [http://voltia.ifrance.com/content/view/dive-calendario.htm dive calendario] [http://demurediablo.ifrance.com/library/html/sisley-eau/ sisley eau de campagne] [http://demurediablo.ifrance.com/library/html/unico/ unico 2004] [http://demurediablo.ifrance.com/library/html/stoccaggio-rifiuti/ stoccaggio rifiuti] [http://girlwho-is.ifrance.com/img/styles/carros.htm carros] [http://yeeden.ifrance.com/img/styles/instint/ instint] [http://girlwho-is.ifrance.com/img/styles/indeo-video.htm indeo video play back] [http://lordsander.ifrance.com/topic/bass-tab.htm bass tab raffaella carra] [http://toofarfrommaybe.ifrance.com/view/new/bobble-ice/ bobble ice age] [http://toofarfrommaybe.ifrance.com/view/new/cucine-classiche/ cucine classiche] [http://girlwho-is.ifrance.com/img/styles/soluzione-shadow.htm soluzione shadow of the colossus] [http://tat-ooin.ifrance.com/resources/articles/la-.htm la 2 gurra mondiale] [http://singaporepets.ifrance.com/library/html/malawi.htm malawi] [http://highbulp.ifrance.com/images/small/peugeot-sw/ peugeot sw] [http://tizolaa.ifrance.com/web/creative-zen/ creative zen micro arancione] [http://noxuhax.ifrance.com/content/view/amstrad-tv.htm amstrad tv 5] [http://oiyaoi.ifrance.com/images/small/qui-a/ qui a le droit] [http://highbulp.ifrance.com/images/small/dub-optika/ dub optika] [http://noxuhax.ifrance.com/content/view/www-portobello.htm www portobello it] [http://deviantrus.ifrance.com/topic/cavo-usb/ cavo usb stampanti] [http://demurediablo.ifrance.com/library/html/pino-daniele/ pino daniele -project] [http://igaros.ifrance.com/lib/dimmi-di/ dimmi di no] [http://yeeden.ifrance.com/img/styles/optin-email/ optin email marketing] [http://voltia.ifrance.com/content/view/mezzoforte.htm mezzoforte] [http://tizolaa.ifrance.com/web/quotazioni-multipla/ quotazioni multipla] [http://yeeden.ifrance.com/img/styles/motori-graupner/ motori graupner] [http://singaporepets.ifrance.com/library/html/bmw-.htm bmw 4 serie] [http://speedofsoun.ifrance.com/text/directory/batterie-z.htm batterie z300] [http://toofarfrommaybe.ifrance.com/view/new/autoradio-mp/ autoradio mp3 slot] [http://deviantrus.ifrance.com/topic/il-dito/ il dito nella piaga] [http://oiyaoi.ifrance.com/images/small/crane-hart/ crane, hart] [http://demurediablo.ifrance.com/library/html/franco-simone/ franco simone] [http://akmokanzen.ifrance.com/images/small/mike-buongiorno.htm mike buongiorno] [http://broadwaylili.ifrance.com/content/view/sorelline.htm sorelline] [http://yeeden.ifrance.com/img/styles/ricettario-bertolini/ ricettario bertolini] [http://highbulp.ifrance.com/images/small/calendario-ilary/ calendario ilary blasi] [http://yeeden.ifrance.com/img/styles/stampanti-laser/ stampanti laser colore a4 samsung] [http://igaros.ifrance.com/lib/pellicola-vetri/ pellicola vetri] [http://deviantrus.ifrance.com/topic/duncan-i/ duncan i believe my heart] [http://toofarfrommaybe.ifrance.com/view/new/keys-viaaccess/ keys viaaccess 007c00] [http://girlwho-is.ifrance.com/img/styles/putas-borrachas.htm putas borrachas] [http://tizolaa.ifrance.com/web/plasma-philips/ plasma philips 42] [http://lovereceier.ifrance.com/lib/roberta-giarusso.htm roberta giarusso nuda] [http://voltia.ifrance.com/content/view/ventole-pc.htm ventole pc] [http://jdawsona.ifrance.com/data/malevil/ malevil] [http://tizolaa.ifrance.com/web/tommasini-hotel/ tommasini hotel] [http://speedofsoun.ifrance.com/text/directory/tamil-songs.htm tamil songs] [http://voltia.ifrance.com/content/view/monitor-benq.htm monitor benq 937] [http://deviantrus.ifrance.com/topic/legacoopbund/ legacoopbund] [http://broadwaylili.ifrance.com/content/view/lorna-papi.htm lorna papi chulo] [http://oiyaoi.ifrance.com/images/small/nikon/ nikon 70 200 2 8] [http://tat-ooin.ifrance.com/resources/articles/aria-mesh.htm aria mesh puma] [http://lovereceier.ifrance.com/lib/calciatore-stranieri.htm calciatore stranieri] [http://girlwho-is.ifrance.com/img/styles/torna-backyard.htm torna backyard wrestling] [http://highbulp.ifrance.com/images/small/c-dragonball/ c18 dragonball x] [http://igaros.ifrance.com/lib/videoclip-eifel/ videoclip eifel 65] [http://toofarfrommaybe.ifrance.com/view/new/barry-white/ barry white cd] [http://girlwho-is.ifrance.com/img/styles/storia-festa.htm storia festa donna] [http://lordsander.ifrance.com/topic/batteria-cordless.htm batteria cordless sagem] [http://toofarfrommaybe.ifrance.com/view/new/harry-potter/ harry potter soundtrack] [http://noxuhax.ifrance.com/content/view/kit-auto.htm kit auto altoparlanti] [http://highbulp.ifrance.com/images/small/canon-fotografia/ canon fotografia] [http://igaros.ifrance.com/lib/aquatica-online/ aquatica online] [http://girlwho-is.ifrance.com/img/styles/castrazione.htm castrazione] [http://tat-ooin.ifrance.com/resources/articles/some-girl.htm some girl] [http://tizolaa.ifrance.com/web/fattoria-maremmana/ fattoria maremmana] [http://deviantrus.ifrance.com/topic/acqua-fredda/ acqua fredda] [http://igaros.ifrance.com/lib/www-provini/ www provini it] [http://girlwho-is.ifrance.com/img/styles/donne-inculate.htm donne inculate da cani] [http://speedofsoun.ifrance.com/text/directory/trucchi-per.htm trucchi per spyro 3] [http://akmokanzen.ifrance.com/images/small/www-sillyrabbit.htm www sillyrabbit com] [http://oiyaoi.ifrance.com/images/small/djv/ dj600v] [http://igaros.ifrance.com/lib/album-foto/ album foto matrimonio] [http://akmokanzen.ifrance.com/images/small/vietnam-hotel.htm vietnam hotel] [http://broadwaylili.ifrance.com/content/view/telegiornale-filmati.htm telegiornale filmati iraq] [http://voltia.ifrance.com/content/view/www-concessionario.htm www concessionario usato it] [http://deviantrus.ifrance.com/topic/djerba-last/ djerba last minute] [http://toofarfrommaybe.ifrance.com/view/new/rex-rd/ rex rd 1871 da] [http://noxuhax.ifrance.com/content/view/isacco-newton.htm isacco newton] [http://demurediablo.ifrance.com/library/html/lacie-/ lacie 50 gb] [http://lordsander.ifrance.com/topic/polski-hip.htm polski hip hop] [http://akmokanzen.ifrance.com/images/small/lego-studios.htm lego studios] [http://yeeden.ifrance.com/img/styles/harley-davidson/ harley davidson] [http://tizolaa.ifrance.com/web/agriturismo-asiago/ agriturismo asiago] [http://girlwho-is.ifrance.com/img/styles/belvedere-amalfi.htm belvedere amalfi] [http://lovereceier.ifrance.com/lib/tanti-giochi.htm tanti giochi sexy] [http://voltia.ifrance.com/content/view/penelope-miller.htm penelope miller] [http://kalian42.ifrance.com/text/directory/elementari-nude/ elementari nude foto] [http://tat-ooin.ifrance.com/resources/articles/gps-portatile.htm gps portatile] [http://tat-ooin.ifrance.com/resources/articles/dainese-jet.htm dainese jet] [http://yeeden.ifrance.com/img/styles/moto-club/ moto club lucca] [http://demurediablo.ifrance.com/library/html/data-bank/ data bank] [http://deviantrus.ifrance.com/topic/lettore-multimedia/ lettore multimedia] [http://oiyaoi.ifrance.com/images/small/luomo-che/ luomo che uccise il suo carnefice] [http://tizolaa.ifrance.com/web/tomi/ tomi] [http://igaros.ifrance.com/lib/valvonauta-verdena/ valvonauta verdena] [http://demurediablo.ifrance.com/library/html/manuale-maya/ manuale maya] [http://singaporepets.ifrance.com/library/html/drops-of.htm drops of jupiter train] [http://noxuhax.ifrance.com/content/view/quattro-foto.htm quattro foto e un sondaggio per far cry] [http://highbulp.ifrance.com/images/small/grazie-di/ grazie di esistere] [http://speedofsoun.ifrance.com/text/directory/ilguerriero.htm ilguerriero] [http://tat-ooin.ifrance.com/resources/articles/immagini-e.htm immagini e nuovi dettagli per motogp 3] [http://tizolaa.ifrance.com/web/le-isole/ le isole canarie] [http://igaros.ifrance.com/lib/w-netsky/ w32 netsky p] [http://girlwho-is.ifrance.com/img/styles/agricoltura-.htm agricoltura - servizi] [http://speedofsoun.ifrance.com/text/directory/www-milano.htm www milano it] [http://jdawsona.ifrance.com/data/soulseek/ soulseek] [http://toofarfrommaybe.ifrance.com/view/new/ca/ ca] [http://yeeden.ifrance.com/img/styles/kendall-henry/ kendall, henry way] [http://singaporepets.ifrance.com/library/html/cerco-lavoro.htm cerco lavoro a catania] [http://toofarfrommaybe.ifrance.com/view/new/oroscopo-capricorno/ oroscopo capricorno] [http://broadwaylili.ifrance.com/content/view/www-bradford.htm www bradford it] [http://demurediablo.ifrance.com/library/html/packard-bell/ packard bell - store play hard disk] [http://girlwho-is.ifrance.com/img/styles/di-salto.htm di salto] {{task}} |
|||
{{Template:split-review}} |
{{Template:split-review}} |
||
In this task, the goal is to find the <em>character</em> length of a string. This means encodings like [[UTF-8]] need to be handled properly, as there is not necessarily a one-to-one relationship between bytes and characters. |
In this task, the goal is to find the <em>character</em> length of a string. This means encodings like [[UTF-8]] need to be handled properly, as there is not necessarily a one-to-one relationship between bytes and characters. |
||
Line 59: | Line 59: | ||
char *p = (char *) string; |
char *p = (char *) string; |
||
while (*p |
while (*p++ != '\0') length++; |
||
return 0; |
return 0; |
||
Line 93: | Line 93: | ||
} |
} |
||
==[[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 |
'''Standard:''' [[ISO C plus plus|ISO C++]] (AKA [[C plus plus 98|C++98]]): |
||
'''Compiler:''' g |
'''Compiler:''' g++ 4.0.2 |
||
#include <string> // note: '''not''' <string.h> |
#include <string> // note: '''not''' <string.h> |
||
Line 124: | Line 124: | ||
'''Platform:''' [[.NET]] |
'''Platform:''' [[.NET]] |
||
'''Language Version:''' 1.0 |
'''Language Version:''' 1.0+ |
||
string s = "Hello, world!"; |
string s = "Hello, world!"; |
||
Line 172: | Line 172: | ||
binary |
binary |
||
: utf8 |
: utf8+ ( str -- str ) |
||
begin |
begin |
||
char |
char+ |
||
dup c@ |
dup c@ |
||
11000000 and |
11000000 and |
||
Line 186: | Line 186: | ||
swap dup c@ |
swap dup c@ |
||
while |
while |
||
utf8 |
utf8+ |
||
swap 1 |
swap 1+ |
||
repeat drop ; |
repeat drop ; |
||
Line 216: | Line 216: | ||
Since Java 1.5, the actual number of characters can be determined by calling the codePointCount method. |
Since Java 1.5, the actual number of characters can be determined by calling the codePointCount method. |
||
String str = "\uD834\uDD2A"; //U |
String str = "\uD834\uDD2A"; //U+1D12A |
||
int length1 = str.length(); //2 |
int length1 = str.length(); //2 |
||
int length2 = str.codePointCount(0, str.length()); //1 |
int length2 = str.codePointCount(0, str.length()); //1 |
||
Line 229: | Line 229: | ||
var len1 = str1.length; //13 |
var len1 = str1.length; //13 |
||
var str2 = "\uD834\uDD2A"; //U |
var str2 = "\uD834\uDD2A"; //U+1D12A represented by a UTF-16 surrogate pair |
||
var len2 = str2.length; //2 |
var len2 = str2.length; //2 |
||
Revision as of 04:53, 20 August 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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 character length of a string. This means encodings like UTF-8 need to be handled properly, as there is not necessarily a one-to-one relationship between bytes and characters.
For byte length, see String Byte Length.
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
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; }
For wide character strings (usually Unicode):
#include <stdio.h> #include <wchar.h> int main(void) { wchar_t *s = L"\x304A\x306F\x3088\x3046"; /* Japanese hiragana ohayou */ size_t length; length = wcslen(s); printf("Length in characters = %d\n", length); printf("Length in bytes = %d\n", sizeof(s) * sizeof(wchar_t)); 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!"; // Always in characters == bytes since sizeof(char) == 1 std::string::size_type length = s.length(); // option 1: In Characters/Bytes std::string::size_type size = s.size(); // option 2: In Characters/Bytes }
For wide character strings:
#include <string> int main() { std::wstring s = L"\u304A\u306F\u3088\u3046"; std::wstring::size_type length = s.length(); }
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!")
E
"Hello World".size()
Forth
The 1994 ANS standard does not have any notion of a particular character encoding, although it distinguishes between character and machine-word addresses. (There is some ongoing work on standardizing an "XCHAR" wordset for dealing with strings in particular encodings such as UTF-8.)
Interpreter: ANS Forth
The following code will count the number of UTF-8 characters in a null-terminated string. It relies on the fact that all bytes of a UTF-8 character except the first have the the binary bit pattern "10xxxxxx".
binary : utf8+ ( str -- str ) begin char+ dup c@ 11000000 and 10000000 <> until ; decimal : count-utf8 ( zstr -- n ) 0 begin swap dup c@ while utf8+ swap 1+ repeat drop ;
Haskell
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 most commonly used characters are represented by one 16-bit value, while rarer ones like some mathematical symbols are represented by two.
The length method of String objects gives the number of 16-bit values used to encode a string.
String s = "Hello, world!"; int length = s.length();
Since Java 1.5, the actual number of characters can be determined by calling the codePointCount method.
String str = "\uD834\uDD2A"; //U+1D12A int length1 = str.length(); //2 int length2 = str.codePointCount(0, str.length()); //1
JavaScript
JavaScript encodes strings in UTF-16, which represents each character with one or two 16-bit values. The most commonly used characters are represented by one 16-bit value, while rarer ones like some mathematical symbols are represented by two.
JavaScript has no built-in way to determine how many characters are in a string. However, if the string only contains commonly used characters, the number of characters will be equal to the number of 16-bit values used to represent the characters.
var str1 = "Hello, world!"; var len1 = str1.length; //13 var str2 = "\uD834\uDD2A"; //U+1D12A represented by a UTF-16 surrogate pair var len2 = str2.length; //2
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 any 5.X
my $length = length "Hello, world!";
PHP
$length = strlen('Hello, world!');
PL/SQL
DECLARE string VARCHAR2( 50 ) := 'Hello, world!'; stringlength NUMBER; BEGIN stringlength := length( string ); END;
Python
Interpreter: Python 2.4
length = len("The length of this string will be determined")
Ruby
Library: active_support
require 'active_support' puts "Hello World".chars.length
Scheme
(string-length "Hello world")
Seed7
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 length "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 characters is %d" $s1 [string length $s1]] puts [format "length of \"%s\" in characters is %d" $s2 [string length $s2]]
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
Len(string|varname)
Returns the length of the string|varname 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"
- Programming Tasks
- Solutions by Programming Task
- Split and Needing Review
- ActionScript
- Ada
- AppleScript
- AWK
- C
- C plus plus
- C sharp
- Clean
- ColdFusion
- Common Lisp
- Component Pascal
- E
- Forth
- Haskell
- IDL
- Java
- JavaScript
- JudoScript
- Lua
- MIRC Scripting Language
- OCaml
- Perl
- PHP
- PL/SQL
- Python
- Ruby
- Scheme
- Seed7
- Smalltalk
- Standard ML
- Tcl
- UNIX Shell
- VBScript
- XTalk