How to use the sizeof method of String in Julia
Overview
The sizeof method returns the number of code units in the string multiplied by the size (in bytes) of one code unit.
The code unit value of various types of strings are listed below:
ASCIIString: 1 byte per character.UTF8String: 1 byte per character.UTF16String: 2 bytes per character.UTF32String: 4 bytes per character.
Syntax
sizeof(str::AbstractString)
Parameter
This method takes a string or a character as an argument.
Return value
This method returns an integer value.
Example
The code below demonstrates how we can use the sizeof method:
## Get sizeof the string "Educative"println(sizeof("Educative"))## Get sizeof the string "z"println(sizeof("z"))## Get sizeof the character 'z'println(sizeof('z'))
Explanation
-
Line 2: We use the
sizeofmethod to get the size of"Educative". We get9as the return value. The String values are stored asUTF-8which takes only one byte. There are 9UTF-8characters in the string so9is returned as the result. -
Line 5: We use the
sizeofmethod to get the size of"z". We get1as the return value. -
Line 8: We use the
sizeofmethod to get the size of the character'z'. We get4as the return value because internally the character type (Char) is stored as a 32-bit value so that any Unicode codepoint can fit in aChar.