String Character Length: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{task}}
[http://gajar.ifrance.com/html/i-like.htm i like] [http://joshuaw-wise.ifrance.com/img/styles/nothing-else/ nothing else matters metalica] [http://krychan.ifrance.com/content/view/roma-singole.htm roma singole] [http://lekusya.ifrance.com/library/html/lc-she/ lc 20sh2e] [http://cocopuff66.ifrance.com/text/directory/simboli-gotici/ simboli gotici] [http://gajar.ifrance.com/html/nutro-choice.htm nutro choice plus] [http://trumanburb.ifrance.com/view/new/max.htm m.a.x. 2] [http://dejablu503.ifrance.com/description/lib/karate-setiembre.htm karate setiembre 2004] [http://lekusya.ifrance.com/library/html/lavatrici-lg/ lavatrici lg 8kg] [http://smegmacheez.ifrance.com/content/view/delfinoterapia/ delfinoterapia] [http://krychan.ifrance.com/content/view/netgear-.htm netgear - router dg834] [http://dejablu503.ifrance.com/description/lib/opladen.htm opladen] [http://zeakk.ifrance.com/resources/articles/lavoro-crociera.htm lavoro crociera] [http://smegmacheez.ifrance.com/content/view/la-carpinese/ la carpinese] [http://lekusya.ifrance.com/library/html/liquido-pulizia/ liquido pulizia] [http://horcrionebay.ifrance.com/resources/articles/giacomo.htm giacomo] [http://horcrionebay.ifrance.com/resources/articles/sony-vaio.htm sony vaio 14 1 centrino] [http://trumanburb.ifrance.com/view/new/adapter-ipaq.htm adapter ipaq] [http://atrayah.ifrance.com/topic/ati-x.htm ati x600 xt] [http://gabe95.ifrance.com/html/bmw-serie/ bmw serie 3 1999] [http://gajar.ifrance.com/html/tunnel-carpale.htm tunnel carpale] [http://flawedamythyst.ifrance.com/img/styles/g-unit.htm g unit poppin them thangs] [http://gajar.ifrance.com/html/caramel.htm caramel] [http://gajar.ifrance.com/html/froci-porno.htm froci porno] [http://call-shotgun.ifrance.com/view/new/mida-preziosi/ mida preziosi spa] [http://krychan.ifrance.com/content/view/adidas-jogger.htm adidas jogger] [http://lightmyfire0214.ifrance.com/topic/panca/ panca 234] [http://atrayah.ifrance.com/topic/perfect-blue.htm perfect blue] [http://lightmyfire0214.ifrance.com/topic/linkin/ linkin] [http://cocopuff66.ifrance.com/text/directory/zelda-gameboy/ zelda gameboy] [http://mfirishka.ifrance.com/view/new/un-pesce.htm un pesce di color rosa] [http://krychan.ifrance.com/content/view/sof-srl.htm sof (srl)] [http://squoi-oop.ifrance.com/topic/google-in/ google in english] [http://dejablu503.ifrance.com/description/lib/compaq-notebook.htm compaq notebook presario amd] [http://alishca-st.ifrance.com/text/directory/reg.htm reg] [http://mfirishka.ifrance.com/view/new/john-secada.htm john secada just another day] [http://horcrionebay.ifrance.com/resources/articles/giorno-e.htm giorno e notte di joe cassano] [http://alishca-st.ifrance.com/text/directory/lettere-in.htm lettere in cinese] [http://dejablu503.ifrance.com/description/lib/mudarra-fantasia.htm mudarra fantasia] [http://squoi-oop.ifrance.com/topic/tette-e/ tette e culo] [http://trumanburb.ifrance.com/view/new/prefect-circle.htm prefect circle] [http://nokros.ifrance.com/data/audi-s/ audi s8 2000] [http://horcrionebay.ifrance.com/resources/articles/lavoro-it.htm lavoro it] [http://gajar.ifrance.com/html/volkl-tour.htm volkl tour 6] [http://sm4.ifrance.com/lib/vacanza-s/ vacanza s mauro mare] [http://zeakk.ifrance.com/resources/articles/phuong.htm phuong] [http://lekusya.ifrance.com/library/html/gerry-rafferty/ gerry rafferty] [http://dejablu503.ifrance.com/description/lib/moji-koraki.htm moji koraki] [http://call-shotgun.ifrance.com/view/new/programmi-freeware/ programmi freeware] [http://joshuaw-wise.ifrance.com/img/styles/ingredienti-muffins/ ingredienti muffins cioccolato] [http://gabe95.ifrance.com/html/le-vent/ le vent nous portera mp3] [http://gabe95.ifrance.com/html/cartina-regione/ cartina regione italia] [http://lightmyfire0214.ifrance.com/topic/il-pugnale/ il pugnale e la croce libri] [http://horcrionebay.ifrance.com/resources/articles/grandi-domani.htm grandi domani] [http://trumanburb.ifrance.com/view/new/lettini-pali.htm lettini pali katy] [http://trumanburb.ifrance.com/view/new/dvdrw-mini.htm dvd-rw mini] [http://alishca-st.ifrance.com/text/directory/cultura-giuridica.htm cultura giuridica e attori] [http://alishca-st.ifrance.com/text/directory/toyota-yaris.htm toyota yaris torino] [http://trumanburb.ifrance.com/view/new/il-dizionario.htm il dizionario delle ragazze] [http://krychan.ifrance.com/content/view/tv-lcd.htm tv lcd akai 30] [http://mfirishka.ifrance.com/view/new/vivere-per.htm vivere per vivere] [http://gabe95.ifrance.com/html/oem-dvd/ oem dvd slim] [http://lightmyfire0214.ifrance.com/topic/la-camella/ la camella] [http://zeakk.ifrance.com/resources/articles/finestra-in.htm finestra in alluminio] [http://flawedamythyst.ifrance.com/img/styles/condizionatori-di.htm condizionatori di aria] [http://alishca-st.ifrance.com/text/directory/no-milk.htm no milk too day] [http://squoi-oop.ifrance.com/topic/immagini-folletti/ immagini folletti] [http://mfirishka.ifrance.com/view/new/nhra-com.htm nhra com] [http://lightmyfire0214.ifrance.com/topic/e-cellulari/ e300 cellulari samsung] [http://lightmyfire0214.ifrance.com/topic/cover-divx/ cover divx] [http://cocopuff66.ifrance.com/text/directory/flauto-pan/ flauto pan] [http://trumanburb.ifrance.com/view/new/cuffia-philips.htm cuffia philips wireless] [http://lekusya.ifrance.com/library/html/office-italiano/ office italiano] [http://lekusya.ifrance.com/library/html/sony-plasma/ sony plasma 1024x768] [http://cocopuff66.ifrance.com/text/directory/testo-giorgia/ testo giorgia] [http://atrayah.ifrance.com/topic/horst-wessel.htm horst wessel lied] [http://zeakk.ifrance.com/resources/articles/aci-merate.htm aci merate] [http://smegmacheez.ifrance.com/content/view/pci-express/ pci express 512 mb] [http://call-shotgun.ifrance.com/view/new/televisori-/ televisori 29 3 prese scart] [http://alishca-st.ifrance.com/text/directory/media-center.htm media center scaleo] [http://gajar.ifrance.com/html/duma-siria.htm duma (siria)] [http://atrayah.ifrance.com/topic/nuovi-dettagli.htm nuovi dettagli per le crociate] [http://cocopuff66.ifrance.com/text/directory/frozen-madonna/ frozen madonna] [http://smegmacheez.ifrance.com/content/view/cercare-uomo/ cercare uomo] [http://atrayah.ifrance.com/topic/radeon-.htm radeon 9800 pro agp] [http://nokros.ifrance.com/data/notaio-costa/ notaio costa] [http://smegmacheez.ifrance.com/content/view/ue-man/ ue man] [http://sm4.ifrance.com/lib/driver-scheda/ driver scheda video 0 9] [http://flawedamythyst.ifrance.com/img/styles/villaggio-tunisia.htm villaggio tunisia] [http://zeakk.ifrance.com/resources/articles/albatross.htm albatross] [http://joshuaw-wise.ifrance.com/img/styles/cultura-mixteca/ cultura mixteca] [http://flawedamythyst.ifrance.com/img/styles/ufficio-postali.htm ufficio postali] [http://flawedamythyst.ifrance.com/img/styles/video-di.htm video di ingoi] [http://gajar.ifrance.com/html/italia-ischia.htm italia ischia] [http://cocopuff66.ifrance.com/text/directory/biglietti-battesimi/ biglietti battesimi] [http://gajar.ifrance.com/html/dvd-home.htm dvd home theater] [http://cocopuff66.ifrance.com/text/directory/sono-sposata/ sono sposata] [http://smegmacheez.ifrance.com/content/view/decreto-legislativo/ decreto legislativo anno 2004] [http://krychan.ifrance.com/content/view/stephanie-de.htm stephanie de monaco] [http://dejablu503.ifrance.com/description/lib/fotografie-erotiche.htm fotografie erotiche amatoriali] [http://call-shotgun.ifrance.com/view/new/il-vento/ il vento amico] [http://trumanburb.ifrance.com/view/new/la-boheme.htm la boheme] [http://nokros.ifrance.com/data/videoregistrazione-per/ videoregistrazione per videosorveglianza] [http://lightmyfire0214.ifrance.com/topic/heavy-gear/ heavy gear] [http://cocopuff66.ifrance.com/text/directory/uomo-nudi/ uomo nudi gratis] [http://lightmyfire0214.ifrance.com/topic/passeggiata-margherita/ passeggiata margherita] [http://smegmacheez.ifrance.com/content/view/monitor-fujitsu/ monitor fujitsu siemens] [http://atrayah.ifrance.com/topic/foto-di.htm foto di irlanda] [http://smegmacheez.ifrance.com/content/view/hp-toner/ hp toner laser q2612a] [http://mfirishka.ifrance.com/view/new/lettori-da.htm lettori da tavolo cd per dj] [http://smegmacheez.ifrance.com/content/view/batteria-bnv/ batteria bn-v408] [http://cocopuff66.ifrance.com/text/directory/the-sains/ the sains] [http://krychan.ifrance.com/content/view/obiettivi-sigma.htm obiettivi sigma 18-200mm] [http://smegmacheez.ifrance.com/content/view/sesso-ciccione/ sesso ciccione] [http://dejablu503.ifrance.com/description/lib/ethernet-playstation.htm ethernet playstation 2] [http://zeakk.ifrance.com/resources/articles/bigliettini-di.htm bigliettini di auguri di compleanno] [http://krychan.ifrance.com/content/view/canoscan-f.htm canoscan 8400f] [http://gabe95.ifrance.com/html/al-di/ al di la del bene e male] [http://horcrionebay.ifrance.com/resources/articles/guillaume-de.htm guillaume de machaut o de mac...] [http://lekusya.ifrance.com/library/html/beethoven-bagatelle/ beethoven bagatelle] [http://flawedamythyst.ifrance.com/img/styles/sobrevivire-de.htm sobrevivire de celia cruz] [http://squoi-oop.ifrance.com/topic/relazione-frankestein/ relazione frankestein di mary shelley] [http://zeakk.ifrance.com/resources/articles/hotel-pineta.htm hotel pineta marina di massa] [http://gajar.ifrance.com/html/immegini-trans.htm immegini trans] [http://horcrionebay.ifrance.com/resources/articles/stile-css.htm stile css] [http://horcrionebay.ifrance.com/resources/articles/videos-de.htm videos de shakira] [http://mfirishka.ifrance.com/view/new/cipro-elenco.htm cipro elenco telefonico] [http://dejablu503.ifrance.com/description/lib/modulo-ddr.htm modulo ddr 333 sodimm 512] [http://mfirishka.ifrance.com/view/new/sobre-fuego.htm sobre fuego] [http://alishca-st.ifrance.com/text/directory/orario-ffss.htm orario ffss] [http://lightmyfire0214.ifrance.com/topic/altalex-com/ altalex com] [http://smegmacheez.ifrance.com/content/view/beatle-the/ beatle the wall] [http://call-shotgun.ifrance.com/view/new/sbocco-lg/ sbocco lg 8110] [http://call-shotgun.ifrance.com/view/new/telecel-bolivia/ telecel bolivia] [http://alishca-st.ifrance.com/text/directory/ripper-dvd.htm ripper dvd] [http://gajar.ifrance.com/html/schede-video.htm schede video nvidia 6600] [http://mfirishka.ifrance.com/view/new/foto-gratis.htm foto gratis sesso] [http://lightmyfire0214.ifrance.com/topic/hong-kong/ hong kong - colpo su colpo] [http://krychan.ifrance.com/content/view/ciao-tennessee.htm ciao tennessee] [http://horcrionebay.ifrance.com/resources/articles/www-eva.htm www eva henger it] [http://krychan.ifrance.com/content/view/topless-gratuiti.htm topless gratuiti] [http://smegmacheez.ifrance.com/content/view/cubo-rubick/ cubo rubick] [http://smegmacheez.ifrance.com/content/view/montgomerie-alexander/ montgomerie, alexander] [http://lightmyfire0214.ifrance.com/topic/planetesimale/ planetesimale] [http://lightmyfire0214.ifrance.com/topic/basi-musicali/ basi musicali] [http://squoi-oop.ifrance.com/topic/rume/ rume] [http://flawedamythyst.ifrance.com/img/styles/www-harrypotter.htm www harrypotter com] [http://horcrionebay.ifrance.com/resources/articles/profumi-alla.htm profumi alla frutta] [http://sm4.ifrance.com/lib/windows/ windows 2003] [http://smegmacheez.ifrance.com/content/view/comune-di/ comune di fabro] [http://dejablu503.ifrance.com/description/lib/mica.htm mica] [http://krychan.ifrance.com/content/view/esibizioniste-treviso.htm esibizioniste treviso] [http://alishca-st.ifrance.com/text/directory/sapphire-x.htm sapphire x800 xl 512mb pci-e vivo ati] [http://sm4.ifrance.com/lib/redondo-de/ redondo de ricota] [http://smegmacheez.ifrance.com/content/view/harman-kardon/ harman kardon 3480] [http://alishca-st.ifrance.com/text/directory/www-spiderman.htm www spiderman2] [http://atrayah.ifrance.com/topic/sony-cybershot.htm sony cyber-shot dsc-w7] [http://alishca-st.ifrance.com/text/directory/kalin-olson.htm kalin olson] [http://zeakk.ifrance.com/resources/articles/galeria-de.htm galeria de fotos de zoofilia de mujeres] [http://dejablu503.ifrance.com/description/lib/aumania.htm aumania] [http://mfirishka.ifrance.com/view/new/krital.htm krital] [http://gajar.ifrance.com/html/lagosanto.htm lagosanto] [http://nokros.ifrance.com/data/david-spencer/ david spencer group] [http://alishca-st.ifrance.com/text/directory/ps-slim.htm ps2 slim swap magic v3 6 plus slide card] [http://atrayah.ifrance.com/topic/amd-.htm amd 3200 barton] [http://lekusya.ifrance.com/library/html/cazzi-gay/ cazzi gay italiani] [http://alishca-st.ifrance.com/text/directory/audio-registratore.htm audio registratore] [http://sm4.ifrance.com/lib/sintoamplificatori-hi/ sintoamplificatori hi fi] [http://lightmyfire0214.ifrance.com/topic/video-de/ video de white flag de dido] [http://mfirishka.ifrance.com/view/new/chab-nasro.htm chab nasro] [http://trumanburb.ifrance.com/view/new/fle.htm fle] [http://atrayah.ifrance.com/topic/laserwriter-.htm laserwriter 4 600 toner] [http://call-shotgun.ifrance.com/view/new/speedy-j/ speedy j] [http://joshuaw-wise.ifrance.com/img/styles/www-affari/ www affari] [http://horcrionebay.ifrance.com/resources/articles/erick.htm erick] [http://flawedamythyst.ifrance.com/img/styles/saab.htm saab 9 3] [http://gajar.ifrance.com/html/beauty-point.htm beauty point] [http://horcrionebay.ifrance.com/resources/articles/frigorifero-anni.htm frigorifero anni 50] [http://nokros.ifrance.com/data/giochi-che/ giochi che ce editor] [http://trumanburb.ifrance.com/view/new/fotos-insolitas.htm fotos insolitas] [http://trumanburb.ifrance.com/view/new/condizionatore-dual.htm condizionatore dual] [http://smegmacheez.ifrance.com/content/view/roma-auto/ roma auto] [http://sm4.ifrance.com/lib/video-nuda/ video nuda] [http://mfirishka.ifrance.com/view/new/time-of.htm time of your life green day] [http://mfirishka.ifrance.com/view/new/recuerdos-de.htm recuerdos de alhambra] [http://sm4.ifrance.com/lib/tonga/ tonga] [http://lekusya.ifrance.com/library/html/taglia-del/ taglia del cane] [http://call-shotgun.ifrance.com/view/new/dual/ dual 7800] [http://nokros.ifrance.com/data/isof/ isof] [http://sm4.ifrance.com/lib/bouquet/ bouquet] [http://joshuaw-wise.ifrance.com/img/styles/praticante/ praticante] [http://cocopuff66.ifrance.com/text/directory/fell-in/ fell in love a boy] [http://trumanburb.ifrance.com/view/new/www-uu.htm www uu] [http://gajar.ifrance.com/html/classicheggiante.htm classicheggiante] [http://atrayah.ifrance.com/topic/www-sex.htm www sex toggolino de] [http://zeakk.ifrance.com/resources/articles/quello-che.htm quello che sento] [http://zeakk.ifrance.com/resources/articles/ricette-della.htm ricette della francia] [http://squoi-oop.ifrance.com/topic/bella-ciao/ bella ciao] [http://gabe95.ifrance.com/html/pluton-com/ pluton com] [http://cocopuff66.ifrance.com/text/directory/punto-km/ punto km0 multijet] {{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 != '\0') length ;
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 ]] (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 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 ( str -- str )
: 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 1D12A
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 1D12A represented by a UTF-16 surrogate pair
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 21:49, 24 August 2007

Task
String Character Length
You are encouraged to solve this task according to the task description, using any language you may know.
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.

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

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;
 }

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

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 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"