String Character Length: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{task}}
[http://furrysoap.ifrance.com/gioco-pc/ gioco pc gratis] [http://doctorpinkerton.ifrance.com/lib/donne-russe/ donne russe] [http://cincydj.ifrance.com/images/small/linea-porno/ linea porno gay] [http://furrysoap.ifrance.com/sexy-tv/ sexy tv] [http://bakadeshi.ifrance.com/img/styles/foto-sexy.html foto sexy monica bellucci] [http://competitio.ifrance.com/travestiti-transessuali/ travestiti transessuali] [http://elixirium.ifrance.com/lib/www-girls.html www girls gone wild com] [http://mi-kkiechang.ifrance.com/pornoa-henger.html pornoa henger] [http://tulip123456.ifrance.com/topic/belle-ragazze.html belle ragazze nude] [http://jnk713.ifrance.com/img/styles/foto-donna/ foto donna nuda gratis] [http://competitio.ifrance.com/pamela-anderson/ pamela anderson tommy le] [http://elixirium.ifrance.com/lib/cover-paola.html cover paola turci] [http://bakadeshi.ifrance.com/img/styles/eros-ramaz.html eros ramaz] [http://spirit-psyki.ifrance.com/lib/sexi-ciccione.html sexi ciccione] [http://mi-kkiechang.ifrance.com/foto-gratis.html foto gratis nudo] [http://furrysoap.ifrance.com/donne-nude/ donne nude su internet] [http://cincydj.ifrance.com/images/small/foto-sexy/ foto sexy di britney spears] [http://elixirium.ifrance.com/lib/pamela-anderson.html pamela anderson e tommy lee film hard gr] [http://mi-kkiechang.ifrance.com/sex-scy.html sex scy] [http://cincydj.ifrance.com/images/small/marina-sexy/ marina sexy gratis] [http://adrienneee.ifrance.com/blog/http-www/ http www sexzool com] [http://spirit-psyki.ifrance.com/lib/sesso-gay.html sesso gay torino] [http://adrienneee.ifrance.com/blog/bestiality-sex/ bestiality sex] [http://spirit-psyki.ifrance.com/lib/canalis-su.html canalis su max] [http://cincydj.ifrance.com/images/small/clitoride/ clitoride] [http://furrysoap.ifrance.com/www-babeporno/ www babeporno net] [http://furrysoap.ifrance.com/viet-girl/ viet girl] [http://tulip123456.ifrance.com/topic/temi-gratis.html temi gratis per nokia] [http://doctorpinkerton.ifrance.com/lib/porno-toons/ porno toons] [http://adrienneee.ifrance.com/blog/video-porno/ video porno vip] [http://competitio.ifrance.com/vido-porno/ vido porno gratis] [http://competitio.ifrance.com/naked-gymnasts/ naked gymnasts] [http://tulip123456.ifrance.com/topic/pamela-anderson.html pamela anderson nude] [http://mi-kkiechang.ifrance.com/ragazze-di.html ragazze di cracovia] [http://adrienneee.ifrance.com/blog/duhamel-georges/ duhamel, georges] [http://competitio.ifrance.com/ragazze-torturate/ ragazze torturate] [http://bakadeshi.ifrance.com/img/styles/racconto-eros.html racconto eros] [http://tulip123456.ifrance.com/topic/traduzione-lyrics.html traduzione lyrics fuck it] [http://end14april.ifrance.com/blog/digimon-xxx.html digimon xxx] [http://jnk713.ifrance.com/img/styles/video-pornostar/ video pornostar] [http://tulip123456.ifrance.com/topic/www-xnxx.html www xnxx free hardcore sex] [http://bakadeshi.ifrance.com/img/styles/bambola-ramona.html bambola ramona porno figa] [http://mi-kkiechang.ifrance.com/invio-sms.html invio sms gratis senza registrazione] [http://cincydj.ifrance.com/images/small/hardcore-gratis/ hardcore gratis] [http://end14april.ifrance.com/blog/gay-free.html gay free] [http://cincydj.ifrance.com/images/small/denise-playboy/ denise playboy] [http://doctorpinkerton.ifrance.com/lib/pornostar-lollipop/ pornostar lollipop] [http://cincydj.ifrance.com/images/small/peliculas-de/ peliculas de porno] [http://mi-kkiechang.ifrance.com/perego-paola.html perego paola nuda] [http://doctorpinkerton.ifrance.com/lib/pornografico/ pornografico] [http://spirit-psyki.ifrance.com/lib/foto-lesbo.html foto lesbo] [http://furrysoap.ifrance.com/gay-gallerie/ gay gallerie di sesso gratuite] [http://doctorpinkerton.ifrance.com/lib/francesex/ francesex] [http://elixirium.ifrance.com/lib/puttane-sesso.html puttane sesso e fantasia] [http://adrienneee.ifrance.com/blog/sesso-con/ sesso con nonna] [http://cincydj.ifrance.com/images/small/video-pompini/ video pompini gratis] [http://competitio.ifrance.com/fotos-pornograficas/ fotos pornograficas] [http://spirit-psyki.ifrance.com/lib/frogsex-com.html frogsex com] [http://cincydj.ifrance.com/images/small/pornoxx/ pornoxx] [http://furrysoap.ifrance.com/tutto-gratis/ tutto gratis film porno] [http://doctorpinkerton.ifrance.com/lib/sms-d/ sms d amore gratis] [http://spirit-psyki.ifrance.com/lib/bi-and.html bi and gay group] [http://mi-kkiechang.ifrance.com/sexy-nagma.html sexy nagma] [http://mi-kkiechang.ifrance.com/spiagge-x.html spiagge x nudisti italiane] [http://cincydj.ifrance.com/images/small/film-cavalli/ film cavalli fuck] [http://adrienneee.ifrance.com/blog/www-ridigratis/ www ridigratis it] [http://adrienneee.ifrance.com/blog/gratis-k/ gratis k key seca html] [http://furrysoap.ifrance.com/full-metal/ full metal panic hentai] [http://doctorpinkerton.ifrance.com/lib/sex-vree/ sex vree live and watch] [http://competitio.ifrance.com/sexmovies-com/ sexmovies com] [http://mi-kkiechang.ifrance.com/giochi-erotici.html giochi erotici di super eva] [http://adrienneee.ifrance.com/blog/tgirls/ tgirls] [http://adrienneee.ifrance.com/blog/flowers-of/ flowers of hentai] [http://cincydj.ifrance.com/images/small/venier-topless/ venier topless] [http://adrienneee.ifrance.com/blog/nonno-sex/ nonno sex fuking] [http://bakadeshi.ifrance.com/img/styles/ragazzini-gay.html ragazzini gay] [http://furrysoap.ifrance.com/donna-nuda/ donna nuda gratis] [http://end14april.ifrance.com/blog/pociciones-del.html pociciones del sexso] [http://tulip123456.ifrance.com/topic/tris-sexy.html tris sexy] [http://elixirium.ifrance.com/lib/porno-kiks.html porno kiks] [http://doctorpinkerton.ifrance.com/lib/transex-escort/ transex escort] [http://bakadeshi.ifrance.com/img/styles/rectal-exam.html rectal exam sex] [http://doctorpinkerton.ifrance.com/lib/fuck-you/ fuck you all mp3] [http://furrysoap.ifrance.com/spiagge-italiane/ spiagge italiane x nudismo] [http://end14april.ifrance.com/blog/girlsaloud-sound.html girlsaloud sound of the underground] [http://spirit-psyki.ifrance.com/lib/vagina-pelosa.html vagina pelosa] [http://elixirium.ifrance.com/lib/video-di.html video di donne sex gratis sul p c] [http://mi-kkiechang.ifrance.com/sesso-lesbiche.html sesso lesbiche] [http://cincydj.ifrance.com/images/small/lo-mejor/ lo mejor del porno] [http://doctorpinkerton.ifrance.com/lib/ladro-di/ ladro di sesso] [http://cincydj.ifrance.com/images/small/ragazze-nude/ ragazze nude in cam] [http://tulip123456.ifrance.com/topic/borges-jorge.html borges, jorge luis] [http://tulip123456.ifrance.com/topic/catrina-nuda.html catrina nuda] [http://bakadeshi.ifrance.com/img/styles/corsi-on.html corsi on line gratis] [http://adrienneee.ifrance.com/blog/famosos-desnudos/ famosos desnudos] [http://elixirium.ifrance.com/lib/donne-amanti.html donne amanti del sesso orale in lombardia] [http://doctorpinkerton.ifrance.com/lib/sex-hentai/ sex hentai] [http://competitio.ifrance.com/orgasmo-com/ orgasmo com] [http://doctorpinkerton.ifrance.com/lib/sito-gay/ sito gay] [http://adrienneee.ifrance.com/blog/foto-ragazze/ foto ragazze siliconate gratis] [http://furrysoap.ifrance.com/pornostar-brigitta/ pornostar brigitta] [http://end14april.ifrance.com/blog/gaia-zucchi.html gaia zucchi nuda] [http://furrysoap.ifrance.com/www-sexso/ www sexso] [http://mi-kkiechang.ifrance.com/foto-di.html foto di sesso senza connessione] [http://competitio.ifrance.com/donne-moi/ donne moi le code de sky italia] [http://mi-kkiechang.ifrance.com/gay-feet.html gay feet porn] [http://doctorpinkerton.ifrance.com/lib/midi-tropical/ midi tropical gratis] [http://jnk713.ifrance.com/img/styles/base-audio/ base audio la nostra vita eros ramazzotti] [http://elixirium.ifrance.com/lib/foto-di.html foto di gay attivi] [http://adrienneee.ifrance.com/blog/puttana-gratis/ puttana gratis] [http://jnk713.ifrance.com/img/styles/tariffa-doganale/ tariffa doganale] [http://mi-kkiechang.ifrance.com/hentai-po.html hentai po] [http://elixirium.ifrance.com/lib/seks-anal.html seks anal] [http://cincydj.ifrance.com/images/small/manga-video/ manga video gratis] [http://bakadeshi.ifrance.com/img/styles/jose-carreros.html jose carreros] [http://tulip123456.ifrance.com/topic/gay-maturi.html gay maturi foto] [http://tulip123456.ifrance.com/topic/racconti-erotici.html racconti erotici incesti] [http://mi-kkiechang.ifrance.com/donne-di.html donne di classe] [http://elixirium.ifrance.com/lib/belle-donne.html belle donne mature] [http://spirit-psyki.ifrance.com/lib/sex-free.html sex free movie] [http://bakadeshi.ifrance.com/img/styles/basi-midi.html basi midi ultime canzoni gratis] [http://mi-kkiechang.ifrance.com/baci-vip.html baci vip lesbo] [http://adrienneee.ifrance.com/blog/www-belle/ www belle tette com] [http://mi-kkiechang.ifrance.com/grosse-tette.html grosse tette] [http://doctorpinkerton.ifrance.com/lib/film-gratis/ film gratis scaricare] [http://competitio.ifrance.com/varese-donne/ varese donne] [http://elixirium.ifrance.com/lib/suonerie-cellulari.html suonerie cellulari gratis] [http://jnk713.ifrance.com/img/styles/chat-gay/ chat gay gratuita roma] [http://competitio.ifrance.com/donne-anziani/ donne anziani] [http://cincydj.ifrance.com/images/small/gioco-sexy/ gioco sexy gratis] [http://adrienneee.ifrance.com/blog/nakeed/ nakeed] [http://furrysoap.ifrance.com/hentai-puzzle/ hentai puzzle 2] [http://elixirium.ifrance.com/lib/teen-sesso.html teen sesso] [http://furrysoap.ifrance.com/nudistas/ nudistas] [http://cincydj.ifrance.com/images/small/pimpinela-medley/ pimpinela medley gratis] [http://tulip123456.ifrance.com/topic/gay-maschi.html gay maschi foto] [http://bakadeshi.ifrance.com/img/styles/porno-immaggini.html porno immaggini] [http://furrysoap.ifrance.com/japan-xxx/ japan xxx] [http://bakadeshi.ifrance.com/img/styles/foto-annunci.html foto annunci sexi] [http://doctorpinkerton.ifrance.com/lib/orgasmi-femminili/ orgasmi femminili] [http://adrienneee.ifrance.com/blog/foto-gratis/ foto gratis di maschi nudi] [http://furrysoap.ifrance.com/transex-a/ transex a roma] [http://elixirium.ifrance.com/lib/cartoline-virtuali.html cartoline virtuali sexy] [http://end14april.ifrance.com/blog/www-sexy.html www sexy com br] [http://mi-kkiechang.ifrance.com/video-animal.html video animal sex] [http://jnk713.ifrance.com/img/styles/giochi-hentai/ giochi hentai gratis] [http://mi-kkiechang.ifrance.com/porno-senza.html porno senza rischi] [http://spirit-psyki.ifrance.com/lib/eros-e.html eros e hard] [http://elixirium.ifrance.com/lib/analisi-cliniche.html analisi cliniche] [http://doctorpinkerton.ifrance.com/lib/ragazze-gallarate/ ragazze gallarate] [http://doctorpinkerton.ifrance.com/lib/sesso-grayis/ sesso grayis] [http://jnk713.ifrance.com/img/styles/anal-girls/ anal girls watchs] [http://doctorpinkerton.ifrance.com/lib/fotos-desnudas/ fotos desnudas de zanetti] [http://cincydj.ifrance.com/images/small/midi-liscio/ midi liscio gratis] [http://bakadeshi.ifrance.com/img/styles/sesso-tv.html sesso tv] [http://bakadeshi.ifrance.com/img/styles/foto-misex.html foto misex 2004] [http://end14april.ifrance.com/blog/global-playboys.html global playboys the sound of san francis] [http://mi-kkiechang.ifrance.com/per-donne.html per donne] [http://jnk713.ifrance.com/img/styles/porno-natura/ porno natura] [http://jnk713.ifrance.com/img/styles/scaricare-canzoni/ scaricare canzoni gratis] [http://doctorpinkerton.ifrance.com/lib/video-hard/ video hard di donne famose] [http://spirit-psyki.ifrance.com/lib/penetracion-anal.html penetracion anal] [http://furrysoap.ifrance.com/companeros/ companeros] [http://bakadeshi.ifrance.com/img/styles/star-sex.html star sex] [http://end14april.ifrance.com/blog/sesso-gay.html sesso gay immagini] [http://bakadeshi.ifrance.com/img/styles/girls-gone.html girls gone wild america uncovered] [http://competitio.ifrance.com/famose-xxx/ famose xxx] [http://furrysoap.ifrance.com/video-topless/ video topless] [http://competitio.ifrance.com/massaggiatrici-porno/ massaggiatrici porno] [http://jnk713.ifrance.com/img/styles/spiaggie-nudiste/ spiaggie nudiste] [http://furrysoap.ifrance.com/annunci-erotici/ annunci erotici o di sesso] [http://cincydj.ifrance.com/images/small/donne-vedove/ donne vedove] [http://competitio.ifrance.com/photo-anales/ photo anales] [http://mi-kkiechang.ifrance.com/www-raconti.html www raconti erotici it] [http://bakadeshi.ifrance.com/img/styles/free-sex.html free sex] [http://jnk713.ifrance.com/img/styles/sesso-in/ sesso in testa] [http://mi-kkiechang.ifrance.com/www-fotoporno.html www fotoporno com] [http://end14april.ifrance.com/blog/adult-poker.html adult poker] [http://elixirium.ifrance.com/lib/fiesta-pluma.html fiesta pluma gay] [http://jnk713.ifrance.com/img/styles/sexj-shop/ sexj shop] [http://end14april.ifrance.com/blog/www-foto.html www foto porno gratis donne mature] [http://elixirium.ifrance.com/lib/culo-gratis.html culo gratis] [http://tulip123456.ifrance.com/topic/foto-manga.html foto manga porno] [http://mi-kkiechang.ifrance.com/mutu-foto.html mutu foto sexy] [http://competitio.ifrance.com/modal-women/ modal women sexy] [http://jnk713.ifrance.com/img/styles/raconti-incesto/ raconti incesto] [http://mi-kkiechang.ifrance.com/interno-analogico.html interno analogico modem] [http://spirit-psyki.ifrance.com/lib/giochi-per.html giochi per pc da scaricare gratis] [http://end14april.ifrance.com/blog/pps-gratis.html pps gratis] [http://mi-kkiechang.ifrance.com/nurses-hentai.html nurses hentai xxx] [http://doctorpinkerton.ifrance.com/lib/gay-cazzi/ gay cazzi grossi e duri] [http://furrysoap.ifrance.com/keys-canal/ keys canal] [http://jnk713.ifrance.com/img/styles/child-porno/ child porno mpg] [http://mi-kkiechang.ifrance.com/chat-lesbo.html chat lesbo] [http://elixirium.ifrance.com/lib/oldsex.html oldsex] [http://jnk713.ifrance.com/img/styles/norton-antivirus/ norton antivirus gratis] [http://bakadeshi.ifrance.com/img/styles/koi-mil.html koi mil gaya] [http://end14april.ifrance.com/blog/trucchi-gratis.html trucchi gratis ps2] [http://elixirium.ifrance.com/lib/fotos-en.html fotos en playas nudistas] [http://cincydj.ifrance.com/images/small/foto-mature/ foto mature sexi] {{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 16:36, 27 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"