String Character Length: Difference between revisions

From Rosetta Code
Content deleted Content added
mNo edit summary
m Stupid case-sensitivity.
 
(67 intermediate revisions by 15 users not shown)
Line 1: Line 1:
#REDIRECT [[String length]]
[http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=94 jazz ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0a vicodin] [http://students.hsc.unt.edu/housing/item.cfm?type=2896 zyban online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=178 ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a01 free sagem ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1346 diazepam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=222 free sharp ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a1d cingular ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30230 but adipex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=138 verizon ringtones] [http://wc1.worldcrossing.com/WebX/.1de609fe free qwest ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e4 cheap clonazepam] [http://wc1.worldcrossing.com/WebX/.1de60a31 zanaflex online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=106 free mtv ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a1a free mtv ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a15 free kyocera ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=218 sony ericsson ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=132 tenuate online] [http://students.hsc.unt.edu/housing/item.cfm?type=2904 real ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=137 valium online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=168 cheap paxil] [http://library.cshl.edu/wp/vb/member.php?u=1367 didrex online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30295 order sildenafil] [http://wc1.worldcrossing.com/WebX/.1de609f7 free nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f3 midi ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1348 cheap ativan] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=337 xanax] [http://wc1.worldcrossing.com/WebX/.1de609e6 buy cyclobenzaprine] [http://library.cshl.edu/wp/vb/member.php?u=1360 order lorazepam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=162 alltel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=176 didrex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=204 mp3 ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2911 free cingular ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a07 free tracfone ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30241 xanax online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=356 free tracfone ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=111 norco online] [http://wc1.worldcrossing.com/WebX/.1de60a13 order fioricet] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=191 lorazepam online] [http://library.cshl.edu/wp/vb/member.php?u=1369 free free ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2858 viagra online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=224 free wwe ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2852 xanax online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30307 cheap zoloft] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30331 free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a27 sildenafil] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=145 xenical online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30339 samsung ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30294 prozac online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=176 buy ultracet] [http://library.cshl.edu/wp/vb/member.php?u=1352 buy norco] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=189 but lipitor] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=362 motorola ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=224 free sagem ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=101 meridia online] [http://wc1.worldcrossing.com/WebX/.1de609ee but hydrocodone] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30244 diazepam online] [http://wc1.worldcrossing.com/WebX/.1de609e0 cheap ativan] [http://students.hsc.unt.edu/housing/item.cfm?type=2861 norco online] [http://students.hsc.unt.edu/housing/item.cfm?type=2877 cheap prozac] [http://news.engin.brown.edu/forums/thread-view.asp?tid=216 sonyericsson ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=360 free qwest ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=190 lisinopril online] [http://wc1.worldcrossing.com/WebX/.1de60a10 cheap didrex] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=228 order ultram] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=76 cialis] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30259 cheap alprazolam] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=359 nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e5 cool ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2888 cheap lortab] [http://library.cshl.edu/wp/vb/member.php?u=1373 free nextel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=216 zyban online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=206 pharmacy online online] [http://library.cshl.edu/wp/vb/member.php?u=1344 cheap xanax] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=344 ativan] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30345 sprint ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30248 cheap ativan] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=131 free sprint ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2871 wellbutrin online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=146 zanaflex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=154 carisoprodol online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30280 propecia online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=118 buy prozac] [http://library.cshl.edu/wp/vb/member.php?u=1341 valium online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=144 cheap xanax] [http://students.hsc.unt.edu/housing/item.cfm?type=2905 motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=112 ortho] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=223 wellbutrin online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=169 cheap levitra] [http://news.engin.brown.edu/forums/thread-view.asp?tid=156 fioricet online] [http://library.cshl.edu/wp/vb/member.php?u=1377 samsung ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30312 ortho online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30356 free cool ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f0 cheap lorazepam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=187 celexa online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30230 tramadol online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=211 free polyphonic ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=214 zanaflex online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=110 nokia ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=189 ortho online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=237 free samsung ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=151 soma online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=99 cheap lorazepam] [http://students.hsc.unt.edu/housing/item.cfm?type=2912 sagem ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2907 sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a17 buy lipitor] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30334 free mp3 ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=234 free sprint ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=141 buy vigrx] [http://wc1.worldcrossing.com/WebX/.1de60a23 free polyphonic ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a1f lisinopril online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=197 free mp3 ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1349 viagra online] [http://library.cshl.edu/wp/vb/member.php?u=1382 cingular ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30310 cheap tenuate] [http://students.hsc.unt.edu/housing/item.cfm?type=2922 free kyocera ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=206 qwest ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f9 norco online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=351 buy clonazepam] [http://students.hsc.unt.edu/housing/item.cfm?type=2878 cheap sildenafil] [http://students.hsc.unt.edu/housing/item.cfm?type=2902 nextel ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=357 funny ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1347 cialis] [http://library.cshl.edu/wp/vb/member.php?u=1365 ultracet online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=210 sprint ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=158 cheap diazepam] [http://wc1.worldcrossing.com/WebX/.1de609ea free free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a25 free samsung ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=88 free ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1358 cheap hydrocodone] [http://library.cshl.edu/wp/vb/member.php?u=1339 tramadol online] [http://students.hsc.unt.edu/housing/item.cfm?type=2868 cheap ambien] [http://news.engin.brown.edu/forums/thread-view.asp?tid=228 mtv ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=104 motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=89 funny ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=211 music ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=80 free cool ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1388 fioricet online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=361 real ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=172 online lorazepam] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=333 cheap soma] [http://news.engin.brown.edu/forums/thread-view.asp?tid=182 cheap clomid] [http://library.cshl.edu/wp/vb/member.php?u=1355 but clonazepam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=205 cheap paxil] [http://news.engin.brown.edu/forums/thread-view.asp?tid=184 lisinopril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=124 free samsung ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a04 sonyericsson ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=115 cheap phentermine] [http://library.cshl.edu/wp/vb/member.php?u=1387 alltel ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a05 free sprint ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=167 clonazepam online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30283 buy didrex] [http://news.engin.brown.edu/forums/thread-view.asp?tid=157 cheap ultram] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=230 free verizon ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30309 cheap celexa] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=348 cheap norco] [http://wc1.worldcrossing.com/WebX/.1de60a11 buy diethylpropion] [http://news.engin.brown.edu/forums/thread-view.asp?tid=188 cheap tenuate] [http://news.engin.brown.edu/forums/thread-view.asp?tid=212 verizon ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30308 lipitor online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=82 diazepam online] [http://library.cshl.edu/wp/vb/member.php?u=1380 free verizon ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=163 cheap adipex] [http://students.hsc.unt.edu/housing/item.cfm?type=2859 cheap meridia] [http://news.engin.brown.edu/forums/thread-view.asp?tid=209 samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=170 cingular ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=78 clomid online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30298 cheap lisinopril] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=74 carisoprodol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=352 paxil online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=227 ericsson ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1386 mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=69 cheap albuterol] [http://wc1.worldcrossing.com/WebX/.1de60a12 ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=171 ambien online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=188 levitra online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=83 didrex online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30358 midi ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2879 clomid online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=186 cheap lipitor] [http://news.engin.brown.edu/forums/thread-view.asp?tid=215 free sagem ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=346 meridia online] [http://students.hsc.unt.edu/housing/item.cfm?type=2923 free sony ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30237 valium online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=171 clomid online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=184 hoodia online] [http://wc1.worldcrossing.com/WebX/.1de60a1b music ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30264 but hydrocodone] [http://wc1.worldcrossing.com/WebX/.1de609db but adipex] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30321 cheap hgh] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=90 cheap hgh] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30263 levitra online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=133 free tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=226 free sony ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=117 propecia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=165 vicodin online] [http://library.cshl.edu/wp/vb/member.php?u=1340 soma online] [http://students.hsc.unt.edu/housing/item.cfm?type=2909 verizon ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30252 buy meridia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=221 order vicodin] {{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: