String Byte Length: Difference between revisions

From Rosetta Code
Content deleted Content added
No edit summary
m Stupid case-sensitivity.
 
(77 intermediate revisions by 19 users not shown)
Line 1: Line 1:
#REDIRECT [[String length]]
[http://tegre.blogspot.com casino online] [http://olud-matilda.blogspot.com cheap medication no phentermine prescription] [http://anninator.blogspot.com buy link onlinehtml usersrolro xanax xanax] [http://swordbladedevil.blogspot.com where to buy xanax] [http://docompester.blogspot.com casino gambling online] [http://jemzamia.blogspot.com casino game online] [http://comnimh.blogspot.com casino gambling game online] [http://mpopenker.blogspot.com propecia shedding] [http://m-r-icons.blogspot.com propecia prescription] [http://veelaprincess99.blogspot.com argosy casino] [http://jonnymachinegun.blogspot.com best casino online] [http://theetinygoat.blogspot.com lowest price propecia] [http://zotik-lena.blogspot.com casino online usa] [http://hamadelica.blogspot.com propecia proscar] [http://scooby-duelist.blogspot.com 3d casino free gambling game multiplayer not online rpg] [http://estreeladllsur.blogspot.com casino online promotion] [http://kimichameleon.blogspot.com valium grapefruit] [http://marsianca.blogspot.com meds propecia] [http://ignortd-eears.blogspot.com bonus casino gambling online] [http://rosecoma.blogspot.com casino free slot] [http://idrivedrunk.blogspot.com generic propecia] [http://karen101.blogspot.com buy cheap phentermine online pharmacy] [http://marysiam7.blogspot.com buy xanax without prescription in usa] [http://dvorjanin.blogspot.com day next propecia] [http://lil-dorkwad.blogspot.com casino resort] [http://hexakeras5pod.blogspot.com frontal hair loss propecia] [http://tasha-fox.blogspot.com cheap phentermine xml] [http://oxamyth.blogspot.com casino online play] [http://rockbirthedme.blogspot.com casino free game slot] [http://totoro-journal.blogspot.com cheap 99 phentermine] [http://icancounttog.blogspot.com propecia for less] [http://dreiviertel.blogspot.com casino oklahoma] [http://rouy-hero-died.blogspot.com cheap phentermine no script] [http://ehecrazedhtrmit.blogspot.com valium effects] [http://suicidexlove.blogspot.com ativan blood does in long stay] [http://synnin-viemaa.blogspot.com ativan for dementia] [http://spacedim68.blogspot.com buy cheap phentermine online pharmacy] [http://tonegativeone.blogspot.com casino free game online play] [http://anthemboy8.blogspot.com casino venetian] [http://nikkie-cole.blogspot.com phentermine very cheap] [http://nefariouswraith.blogspot.com cheap overnight phentermine] [http://the-amberlady.blogspot.com cheap phentermine online with cod] [http://goler-sor1z.blogspot.com casino royal] [http://master-z-great.blogspot.com blue casino chip] [http://nika-ru.blogspot.com acne propecia] [http://m-i-a-s-m-a.blogspot.com ativan dosing] [http://sunaseth.blogspot.com purchase cheap phentermine] [http://asaearh.blogspot.com buy online prescription xanax] [http://peace-weasel.blogspot.com cheap 15mg phentermine] [http://camelmenthe.blogspot.com casino online tip] [http://dionysios-r.blogspot.com 37 5 cheap online phentermine] [http://maitybaba.blogspot.com casino new online] [http://bondich.blogspot.com mixing valium and xanax] [http://aynippa.blogspot.com propecia drug] [http://floridian-love.blogspot.com casino online review] [http://sofiefie.blogspot.com casino free game] [http://kenji-no-sorata.blogspot.com casino make money online] [http://iron-island.blogspot.com cheap literacy phentermine] [http://eighty-8088.blogspot.com buying propecia] [http://lavender-dove.blogspot.com california casino indian] [http://the-murderer.blogspot.com barona casino] [http://brat-nikodim.blogspot.com cheap delivered find fl phentermine] [http://ratonhteroad.blogspot.com casino guide] [http://zanuda-juja.blogspot.com casino microgaming online] [http://psmorrison.blogspot.com snorting valium] [http://ivoyt.blogspot.com propecia hair] [http://konstantine01.blogspot.com casino royale] [http://lawrencegillies.blogspot.com ameristar casino] [http://arketon24.blogspot.com casino hotel] [http://hand-stained-xx.blogspot.com casino game] [http://iehf.blogspot.com book casino sport] [http://jeanniexong.blogspot.com casino online tournament] [http://azn-chickadee.blogspot.com buy link onlinehtml xanax xanax00procom] [http://thesam118.blogspot.com casino horseshoe] [http://raezey.blogspot.com bonus casino code online] [http://jimmini.blogspot.com cheap phentermine yellow] [http://jokerupot.blogspot.com casino free game online slot] [http://silly-n.blogspot.com best bonus casino online] [http://asaia.blogspot.com casino palm] [http://normanburke.blogspot.com casino fallsview] [http://s1xbucksamonkey.blogspot.com casino english harbour online] [http://moody-immortal2.blogspot.com buy link online traxto xanax] [http://murdersoul.blogspot.com casino hard rock] [http://enchanted1312.blogspot.com injecting valium] [http://foedu-de-solnil.blogspot.com propecia effects] [http://gorillashaman.blogspot.com ativan drug lorazepam] [http://hclt-events.blogspot.com cheap 37 5 phentermine] [http://vegetnuks.blogspot.com picture propecia result] [http://angelcakez.blogspot.com cheap herbal phentermine] [http://dogzilla30.blogspot.com valium vs vicodin] [http://paperroom.blogspot.com casino gaming online] [http://sakura-watanuki.blogspot.com 37 5mg cheap phentermine] [http://pearlibuttons.blogspot.com buy cheap com h1 phentermine phentermine ripway] [http://jesuisleflet.blogspot.com casino game online poker] [http://falling-anvil.blogspot.com casino gambling line online] [http://seranton.blogspot.com propecia side effects] [http://pashkak0marov.blogspot.com casino riverside] [http://piggy-soldier.blogspot.com borgata casino] [http://techinpet.blogspot.com casino fun online play] [http://xitokiry.blogspot.com casino gambling guide online] [http://kalbongzig64.blogspot.com casino hotel palm] [http://peacedoorball.blogspot.com valium without prescription] [http://meadelante.blogspot.com order propecia online] [http://callsecurity.blogspot.com order cheap phentermine online overnite] [http://solacemysoul.blogspot.com phentermine cheap order by] [http://jessiethebestie.blogspot.com valium iv] [http://soulswallo.blogspot.com casino online slot] [http://jon-a-ross.blogspot.com casino com online] [http://phoenixbrngbrt.blogspot.com bonus casino] [http://pyrebi.blogspot.com casino flash online] [http://winter-gossamer.blogspot.com casino free online slot] [http://aurney.blogspot.com get phentermine cheap] [http://mutantalias.blogspot.com valium use] [http://zebbiejohnson.blogspot.com cheap phentermine pill] [http://mrwolfy49.blogspot.com cheap order phentermine prescription] [http://bear-terry.blogspot.com casino gaming] [http://jeffrorocks21.blogspot.com cheap cod extremely phentermine] [http://roxyobsessed7.blogspot.com buy link nowcom online xanax] [http://pastmagic.blogspot.com casino greektown] [http://smackinghotlips.blogspot.com buy link online sitekreatorcom xanax] [http://lilhaq.blogspot.com casino chip] [http://ekviceaia.blogspot.com indication of valium] [http://machi-neko.blogspot.com cheap prescription diet pill phentermine] [http://ryu-jounetsu.blogspot.com canadian casino online] [http://chuvak-org.blogspot.com valium dosage] [http://sahezzz.blogspot.com casino choctaw] [http://prettyhair24-7.blogspot.com propecia study] [http://ouglova.blogspot.com valium half life] [http://woolygrrl.blogspot.com valium side effects] [http://missensign.blogspot.com buy link onlinefansto xanax] [http://anw66.blogspot.com buy doctor xanax] [http://oltcha.blogspot.com bingo casino free game online play] [http://dirty-ru-lenta.blogspot.com propecia online] [http://codigodor.blogspot.com brand buy online xanax] [http://spirit-of-llama.blogspot.com cheap phentermine without rx] [http://sijalik.blogspot.com casino free online play] [http://rm-ast-pants.blogspot.com buy online upjohn xanax] [http://nekostar3.blogspot.com ativan anxiety] [http://sweetpniangel87.blogspot.com long term side effects of propecia] [http://yell0w-n1njaman.blogspot.com casino hotel las vegas] [http://jewels667.blogspot.com propecia impotence] [http://your-starry-sky.blogspot.com casino new york] [http://skvonk.blogspot.com ativan birth defect] [http://pyr0lys1s.blogspot.com cheap phentermine no prior prescription] [http://firejedi.blogspot.com cheapest cheap phentermine] [http://mywaybackwhen.blogspot.com casino deposit new no online] [http://bakamandy.blogspot.com ativan dose lethal] [http://hwantiig-yg.blogspot.com buy xanax legally] [http://laynamarya.blogspot.com ativan and alcohol] [http://xjenhx.blogspot.com viagra medication prescription levitra cialis propecia] [http://manka-kapak.blogspot.com casino craps online] [http://comradealtmer.blogspot.com casino thunder valley] [http://mafy69.blogspot.com casino online play] [http://danga4ka.blogspot.com casino online wagering] [http://bostvndaoe.blogspot.com ativan forum] [http://ridiaulctions.blogspot.com buy overseas xanax] [http://guess76.blogspot.com casino in las vegas] [http://gatx10258.blogspot.com casino links] [http://femdog.blogspot.com casino on net] [http://nord9maedchen73.blogspot.com ativan for depression] [http://chic-ks.blogspot.com casino golden online palace] [http://apesapoppin.blogspot.com canada cheap phentermine] [http://lola-providence.blogspot.com ativan addiction] [http://lilo-n.blogspot.com buy xanax cod] [http://redfxo.blogspot.com propecia before and after] [http://telnushka.blogspot.com buy cheap lowest phentermine price] [http://tart0938.blogspot.com casino grand online] [http://azu-erlove.blogspot.com casino free game online] [http://yasashisa.blogspot.com valium online] [http://xboct13.blogspot.com ativan attack medication online order panic] [http://the-craftman.blogspot.com buy propecia online] [http://yoshi-in-black.blogspot.com how propecia works] [http://kulhain.blogspot.com buy insurance medication without xanax] [http://kabierac.blogspot.com casino eagle soaring] [http://sand-stars.blogspot.com valium sale] [http://vands88.blogspot.com casino gambling internet online] [http://jupka97.blogspot.com cheap online order phentermine] [http://thedailywash.blogspot.com online casino game] [http://justanotherebel.blogspot.com cheap phentermine no prescription needed] [http://hitm4n.blogspot.com buy buy target xanax xanax] [http://ktbug1373.blogspot.com casino vegas] [http://annugel--h.blogspot.com propecia result] [http://fragolla.blogspot.com casino deposit no] [http://ejreoonster.blogspot.com propecia and woman] [http://gololed.blogspot.com buy link member spacesmsncom xanax] [http://mama-rachelbeth.blogspot.com casino slot] [http://mothrinventor.blogspot.com online prescription propecia] [http://rappture.blogspot.com baldness hair lasercomb loss propecia] [http://llillith.blogspot.com casino news online] [http://tayamarn.blogspot.com casino commerce] [http://angry-sister.blogspot.com casino payout] [http://cetki.blogspot.com addicted to ativan] [http://fau75.blogspot.com casino hard rock seminole] [http://un-fois.blogspot.com casino pechanga] [http://suzulan.blogspot.com propecia sale] [http://nixve.blogspot.com propecia picture] [http://lettersof-love.blogspot.com propecia cost] [http://nmagiietending.blogspot.com buy xanax online pay by check] [http://xelby0.blogspot.com casino fortunelounge online] [http://estarcollector.blogspot.com 2mg buy generic xanax] {{Template:split-review}}
{{task}}

In this task, the goal is to find the <em>byte</em> length of a string. This means encodings like [[UTF-8]] may need to be handled specially, as there is not necessarily a one-to-one relationship between bytes and characters, and some languages recognize this.

For character length, see [[String Character Length]].

==[[4D]]==
[[Category:4D]]

$length:=Length("Hello, world!")

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

==[[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!";
std::string::size_type length = s.length(); // option 1: In Characters/Bytes
std::string::size_type size = s.size(); // option 2: In Characters/Bytes
// In bytes same as above since sizeof(char) == 1
std::string::size_type bytes = s.length() * sizeof(std::string::value_type);
}

For wide character strings:

#include <string>
int main()
{
std::wstring s = L"\u304A\u306F\u3088\u3046";
std::wstring::size_type length = s.length() * sizeof(std::wstring::value_type); // in bytes
}

==[[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!")

==[[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 length method of String objects returns the number of 16-bit values used to encode a string, so the number of bytes can be determined by doubling that number.

String s = "Hello, world!";
int byteCount = s.length() * 2;

An other way to know the byte length of a string is to explicitly specify the charset we desire.

String s = "Hello, world!";
int byteCountUTF16 = s.getByte("UTF-16").length;
int byteCountUTF8 = s.getByte("UTF-8").length;

==[[JavaScript]]==
[[Category:JavaScript]]

JavaScript encodes strings in UTF-16, which represents each character with one or two 16-bit values. The length property of string objects gives the number of 16-bit values used to encode a string, so the number of bytes can be determined by doubling that number.

var s = "Hello, world!";
var byteCount = s.length * 2; //26

==[[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]] 5.8.6

Perl strings are in either the platform's native single-byte encoding (usually ISO 8859-1) or UTF-8. <code>utf8::upgrade</code> (a translation function) has the side effect of returning the resulting byte length, and does nothing else if the string is already UTF-8.
$str = "Hello, world!";
$length = utf8::is_utf8($str) ? utf8::upgrade($str) : length $str;
'''Note:''' Do not <code>use utf8;</code> in this case. That has other side effects, and the functions are available without it.

==[[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]]

string="Hello world"
print string.length
or
puts "Hello World".length

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

(string-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 bytelength "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 bytes is %d" $s1 [string bytelength $s1]]
puts [format "length of \"%s\" in bytes is %d" $s2 [string bytelength $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]]
LenB(string|varname)

Returns the number of bytes required to store a string in memory
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:32, 19 January 2008

Redirect to: