Strings

Literal strings are always of type String:

julia> typeof("hello") 
String

This is also true if they contain UTF-8 characters that cannot be represented in ASCII, as in this example:

julia> typeof("Güdrun") 
String

UTF-16 and UTF-32 are also supported. Strings are contained in double quotes (" ") or triple quotes (""" """). They are immutable, which means that they cannot be altered once they have been defined:

julia> s = "Hello, Julia" 
julia> s[2] = 'z'
ERROR: MethodError: no method matching setindex!(::String, ::Char, ::Int64)

String is a succession, or an array of characters (see the Ranges and arrays section) that can be extracted from the string by indexing it, starting from 1: with str = "Julia"str[1] returns the character 'J', and str[end] returns the character 'a', the last character in the string. The index of the last byte is also given by endof(str), and length() returns the number of characters. These two are different if the string contains multi-byte Unicode characters, for example, endof("Güdrun") gives 7, while length("Güdrun") gives 6.

Using an index less than one or greater than the index of the last byte gives a BoundsError. In general, strings can contain Unicode characters, which can take up to four bytes, so not every index is a valid character index. For example, for str2 = "I am the α: the beginning", we have str2[10], which returns 'u3b1' (the two-byte character representing α), str2[11] returns ERROR: StringIndexError (because this is the second byte of the α character), and str2[12] returns colon (:).

We can see 25 characters. length(str2) returns 25, but the last index given by lastindex(str2) returns 26. For this reason, looping over a string's characters can best be done as an iteration and not by using the index, as follows:

for c in str2 
    println(c) 
end 

A substring can be obtained by taking a range of indices:str[3:5] or using str[3:end], which returns "lia". A string that contains a single character is different from that Char value: 'A' == "A" returns false.

Julia has an elegant string interpolation mechanism for constructing strings: $var inside a string is replaced by the value of var, and $(expr), where expr is an expression, is replaced by its computed value. When a is 2 and b is 3, the following expression "$a * $b = $(a * b)" returns "2 * 3 = 6". If you need to write the $ sign in a string, escape it as $.

You can also concatenate strings with the * operator or with the string() function: "ABC" * "DEF" returns "ABCDEF", and string("abc", "def", "ghi") returns "abcdefghi".

Strings prefixed with : are of type Symbol, such as :green; we already used it in the printstyled function. They are more efficient than strings and are used for IDs or keys. Symbols cannot be concatenated. They should only be used if they are expected to remain constant over the course of the execution of the program.

The String type is very rich, and it has 96 functions defined on it, given by methodswith(String). Some useful methods include the following:

  • replace(string, str1, str2): This changes substrings str1 to str2 in string, for example, replace("Julia","u" => "o") returns "Jolia".
  • split(string, char or [chars]): This splits a string on the specified character or characters, for example, split("34,Tom Jones,Pickwick Street 10,Aberdeen", ',') returns the four strings in an array: ["34","Tom Jones","Pickwick Street 10","Aberdeen"]. If char is not specified, the split is done on space characters (spaces, tabs, newlines, and so on).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.68