String Character Length: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
m (Stupid case-sensitivity.)
 
(69 intermediate revisions by 15 users not shown)
Line 1: Line 1:
#REDIRECT [[String length]]
[http://news.engin.brown.edu/forums/thread-view.asp?tid=215 sagem ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f9 norco online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=360 qwest ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=137 cheap valium] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=209 xenical] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=352 paxil online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=97 cheap lipitor] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=228 order ultram] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=104 motorola ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=204 cheap ortho] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=193 cheap meridia] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=133 tracfone ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a22 phentermine] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=201 free nextel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=179 cheap fioricet] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=185 cheap hydrocodone] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=192 lortab online] [http://wc1.worldcrossing.com/WebX/.1de60a0a vicodin online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=220 free real ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=105 free mp3 ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=217 free mono ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=170 cingular ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=206 free qwest ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=188 cheap levitra] [http://news.engin.brown.edu/forums/thread-view.asp?tid=194 cheap diethylpropion] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=212 cheap propecia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=214 cheap zanaflex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=80 free cool ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=113 paxil online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=234 free sprint ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=181 free ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=87 flexeril] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=100 cheap lortab] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=237 samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=233 sonyericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=175 cheap cyclobenzaprine] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=119 free punk ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=218 qwest ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=349 vicodin online] [http://wc1.worldcrossing.com/WebX/.1de609fe qwest ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=225 kyocera ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=174 cyclobenzaprine online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=186 free jazz ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=171 cheap clomid] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=144 xanax online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=236 free tracfone ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=180 online flexeril] [http://wc1.worldcrossing.com/WebX/.1de609f3 free midi ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a2c cheap ultram] [http://wc1.worldcrossing.com/WebX/.1de60a34 free sony ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0d free wwe ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=207 free real ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=110 free nokia ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=220 free punk ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=203 funny ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=173 free cool ringtones] [http://wc1.worldcrossing.com/WebX/.1de609eb funny ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=83 cheap didrex] [http://wc1.worldcrossing.com/WebX/.1de60a21 paxil online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=172 lorazepam] [http://wc1.worldcrossing.com/WebX/.1de60a1a free mtv ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=71 buy alprazolam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=190 pharmacy online online] [http://wc1.worldcrossing.com/WebX/.1de60a03 free sony ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=201 free free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0b cheap vigrx] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=211 free polyphonic ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=358 mp3 ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a23 free polyphonic ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=213 polyphonic ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=150 tramadol online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=163 cheap adipex] [http://wc1.worldcrossing.com/WebX/.1de60a2b cheap ultracet] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=219 cheap viagra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=82 buy diazepam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=185 cheap zoloft] [http://news.engin.brown.edu/forums/thread-view.asp?tid=192 vigrx] [http://wc1.worldcrossing.com/WebX/.1de60a1f lisinopril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=114 cheap pharmacy online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=75 cheap celexa] [http://wc1.worldcrossing.com/WebX/.1de60a05 sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e4 clonazepam online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=353 levitra] [http://wc1.worldcrossing.com/WebX/.1de60a27 sildenafil] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=140 order vicodin] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=337 online xanax] [http://wc1.worldcrossing.com/WebX/.1de60a2d valium online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=206 pharmacy online online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=126 sildenafil online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=174 cheap wellbutrin] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=160 cheap adipex] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=221 order vicodin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=143 wwe ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=70 alltel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=230 free verizon ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=102 free midi ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=118 buy prozac] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=189 but lipitor] [http://news.engin.brown.edu/forums/thread-view.asp?tid=218 sony ericsson ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=232 sony ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=161 albuterol online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=161 cheap viagra] [http://wc1.worldcrossing.com/WebX/.1de609e1 cheap carisoprodol] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=128 free sony ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a24 propecia online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=200 nexium] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=191 lorazepam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=197 flexeril online] [http://wc1.worldcrossing.com/WebX/.1de60a07 tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=182 cheap clomid] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=205 order paxil] [http://news.engin.brown.edu/forums/thread-view.asp?tid=208 free motorola ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=216 zyban online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=120 qwest ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=109 nextel ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=79 clonazepam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=211 free music ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=226 cheap tramadol] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=333 cheap soma] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=81 buy cyclobenzaprine] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=332 cheap tramadol] [http://news.engin.brown.edu/forums/thread-view.asp?tid=160 ativan online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=68 buy adipex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=108 nexium online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=167 cheap clonazepam] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=90 hgh online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=132 cheap tenuate] [http://wc1.worldcrossing.com/WebX/.1de60a32 zoloft] [http://news.engin.brown.edu/forums/thread-view.asp?tid=223 midi ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=166 alprazolam online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=72 ambien online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=241 free sony ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=184 cheap lisinopril] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=194 free midi ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=74 cheap carisoprodol] [http://wc1.worldcrossing.com/WebX/.1de609f8 free nokia ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=182 free funny ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=176 didrex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=196 buy hgh] [http://wc1.worldcrossing.com/WebX/.1de60a10 cheap didrex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=129 sony ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=339 ultram online] [http://wc1.worldcrossing.com/WebX/.1de609ec hgh online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=85 free ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=156 fioricet] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=76 cialis] [http://wc1.worldcrossing.com/WebX/.1de60a1e flexeril online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=336 cheap carisoprodol] [http://wc1.worldcrossing.com/WebX/.1de609ff free real ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=198 cheap hoodia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=165 but ambien] [http://news.engin.brown.edu/forums/thread-view.asp?tid=186 cheap lipitor] [http://wc1.worldcrossing.com/WebX/.1de609fa online pharmacy] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=88 free ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=146 zanaflex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=153 cheap phentermine] [http://wc1.worldcrossing.com/WebX/.1de60a11 buy diethylpropion] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=240 buy soma] [http://news.engin.brown.edu/forums/thread-view.asp?tid=210 free sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e7 cheap diazepam] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=125 free sharp ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=170 cheap hydrocodone] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=347 adipex] [http://news.engin.brown.edu/forums/thread-view.asp?tid=155 xanax online] [http://wc1.worldcrossing.com/WebX/.1de609e3 cheap clomid] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=356 free tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=222 sharp ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=98 online lisinopril] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=223 wellbutrin online] [http://wc1.worldcrossing.com/WebX/.1de60a15 free kyocera ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=362 free motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=115 phentermine online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=215 zoloft online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=167 cheap carisoprodol] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=164 buy alprazolam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=177 cheap diethylpropion] [http://news.engin.brown.edu/forums/thread-view.asp?tid=200 free nokia ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=69 cheap albuterol] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=357 funny ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=334 valium online] [http://wc1.worldcrossing.com/WebX/.1de60a29 tramadol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=338 online fioricet] [http://wc1.worldcrossing.com/WebX/.1de60a09 cheap viagra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=124 free samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=198 free mtv ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=164 cheap norco] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=199 free music ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=166 buy ativan] [http://news.engin.brown.edu/forums/thread-view.asp?tid=188 cheap tenuate] [http://wc1.worldcrossing.com/WebX/.1de60a17 buy lipitor] [http://wc1.worldcrossing.com/WebX/.1de60a12 free ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=151 soma online] [http://wc1.worldcrossing.com/WebX/.1de609df cheap ambien] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=231 order rivotril] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=343 cialis online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=134 tramadol online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=227 free ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=228 free mtv ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=177 propecia online] [http://wc1.worldcrossing.com/WebX/.1de60a14 free jazz ringtones] {{task}}
{{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.

For byte length, see [[String Byte Length]].

==[[ActionScript]]==
[[Category:ActionScript]]
myStrVar.length()

==[[Ada]]==
[[Category:Ada]]

'''Compiler:''' GCC 4.1.2

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

==[[AppleScript]]==
[[Category:AppleScript]]
count of "Hello World"

==[[AWK]]==
[[Category:AWK]]
From within any code block:
w=length("Hello, world!") # static string example
x=length("Hello," s " world!") # dynamic string example
y=length($1) # input field example
z=length(s) # variable name example
Ad hoc program from command line:
echo "Hello, world!" | awk '{print length($0)}'
From executable script: (prints for every line arriving on stdin)
#!/usr/bin/awk -f
{print"The length of this line is "length($0)}

==[[C]]==
[[Category:C]]

'''Standard:''' [[ANSI C]] (AKA [[C89]]):

'''Compiler:''' GCC 3.3.3

#include <string.h>
int main(void)
{
const char *string = "Hello, world!";
size_t length = strlen(string);
return 0;
}

or by hand:

int main(void)
{
const char *string = "Hello, world!";
size_t length = 0;
char *p = (char *) string;
while (*p != '\0') length ;
return 0;
}

or (for arrays of char only)

#include <stdlib.h>
int main(void)
{
char const s[] = "Hello, world!";
size_t length = sizeof s - 1;
return 0;
}

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 plus plus|C ]]==
[[Category:C plus plus|C ]]

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

'''Compiler:''' g 4.0.2

#include <string> // note: '''not''' <string.h>
int main()
{
std::string s = "Hello, world!";
// 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 sharp|C#]]==
[[Category:C sharp|C#]]

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

string s = "Hello, world!";
int clength = s.Length; // In characters
int blength = System.Text.Encoding.GetBytes(s).length; // In Bytes.

==[[Clean]]==
[[Category:Clean]]

Clean Strings are unboxed arrays of characters. Characters are always a single byte. The function size returns the number of elements in an array.

import StdEnv
strlen :: String -> Int
strlen string = size string
Start = strlen "Hello, world!"

==[[ColdFusion]]==
[[Category:ColdFusion]]

#len("Hello World")#

==[[Common Lisp]]==
[[Category:Common Lisp]]

(length "Hello World")

==[[Component Pascal]]==
[[Category:Component Pascal]]

LEN("Hello, World!")

==[[E]]==
[[Category:E]]

"Hello World".size()

==[[Forth]]==
[[Category:Forth]]

'''Interpreter:''' ANS Forth
CREATE s ," Hello world" \ create string "s"
s C@ ( -- length )

==[[Haskell]]==
[[Category:Haskell]]

'''Interpreter:''' [[GHC | GHCi]] 6.6, [[Hugs]]

'''Compiler:''' [[GHC]] 6.6

strlen = length "Hello, world!"

==[[IDL]]==
[[Category:IDL]]

'''Compiler:''' any IDL compiler should do

length = strlen("Hello, world!")

==[[Java]]==
[[Category:Java]]

Java encodes strings in UTF-16, which represents each character with one or two 16-bit values. The 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]]==
[[Category: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]]==
[[Category:JudoScript]]

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

==[[Lua]]==
[[Category:Lua]]

'''Interpreter:''' [[Lua]] 5.0 or later.

string="Hello world"
length=#string

==[[mIRC Scripting Language]]==
[[Category:mIRC Scripting Language]]

'''Interpreter:''' [[mIRC]]

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

==[[OCaml]]==
[[Category:OCaml]]
'''Interpreter'''/'''Compiler:''' [[Ocaml]] 3.09

String.length "Hello world";;


==[[Perl]]==
[[Category:Perl]]
'''Interpreter:''' [[Perl]] any 5.X

my $length = length "Hello, world!";

==[[PHP]]==
[[Category:PHP]]

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

==[[PL/SQL|PL/SQL]]==
[[Category:PL/SQL|PL/SQL]]

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

==[[Python]]==
[[Category:Python]]

'''Interpreter:''' [[Python]] 2.4

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

==[[Ruby]]==
[[Category:Ruby]]
'''Library:''' [[active_support]]

require 'active_support'
puts "Hello World".chars.length

==[[Scheme]]==
[[Category:Scheme]]

(string-length "Hello world")

==[[Seed7]]==
[[Category:Seed7]]

length("Hello, world!")

==[[Smalltalk]]==
[[Category:Smalltalk]]

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

==[[Standard ML]]==
[[Category:Standard ML]]

'''Interpreter:''' [[Standard ML of New Jersey | SML/NJ]] 110.60, [[Moscow ML]] 2.01 (January 2004)

'''Compiler:''' [[MLton]] 20061107

val strlen = size "Hello, world!";

==[[Tcl]]==
[[Category:Tcl]]

Basic version:

string 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]]==
[[Category:UNIX Shell]]

With external utilities:

'''Interpreter:''' any bourne shell

string='Hello, world!'
length=`echo -n "$string" | wc -c | tr -dc '0-9'`
echo $length # if you want it printed to the terminal

With SUSv3 parameter expansion modifier:

'''Interpreter:''' [[Almquist SHell]] (NetBSD 3.0), [[Bourne Again SHell]] 3.2, [[Korn SHell]] (5.2.14 99/07/13.2), [[Z SHell]]

string='Hello, world!'
length="${#string}"
echo $length # if you want it printed to the terminal


==[[VBScript]]==
[[Category:VBScript]]
Len(string|varname)

Returns the length of the string|varname
Returns null if string|varname is null

==[[xTalk]]==
[[Category:xTalk]]

'''Interpreter:''' HyperCard

put the length of "Hello World"

or

put the number of characters in "Hello World"

Latest revision as of 19:31, 19 January 2008

Redirect to: