String in Julia is a finite sequence of characters. The string can consist of numerals, common punctual symbols, a single word, a group of words, or a multi-line paragraph. Julia allows us to use and manipulate the data in the strings in many ways. Julia also offers a few high-level features for the strings. Some of these features are further discussed in this article.
Quoted Strings
Julia offers us to create strings using double-quotes (” “), and triple-quotes (”’ ”’) as well. Double-quoted strings are treated normally but triple-quoted strings have some extra features available to them.
Double-quoted strings
These types of strings are treated normally in Julia like in any other language. Operations like concatenation and interpolation are allowed in double-quoted strings.
Julia
# create three double-quoted strings s1 = "Geeks" s2 = "for" s3 = "geeks" # concatenating strings s = "s1, s2, s3" |
Triple-quoted strings
These types of strings have special behaviors in Julia which are helpful to create long blocks of text. Triple-quoted strings are useful to use in codes that are indented because they recognize new lines.
Julia
# create a triple-quoted string str = """ Geeks, for, geeks """ |
A new line after the first triple quotes is not considered:
Julia
# create string literal """ Geeks""" # create string literal with new line """ Geeks""" |
Interpolation of strings
Doing concatenation on strings sometimes can get inconvenient, to tackle this Julia offers interpolation into string literals using $.
Julia
# create three strings s1 = "Geeks" s2 = "for" s3 = "geeks" # interpolation using $ "$s1 $s2 $s3" |
In Julia, the interpolation operation can be done in a part of a string literal using parentheses:
Julia
# interpolation using parentheses "2 + 4 = $(2 + 4)" |
Escaped strings
To include any character in the string, we have to place a backslash (\) before it:
Julia
# placing $ in string print ( "I want 1000\$ in my bank account" ) |
We can also escape double-quotes using backslash:
Julia
# escaping double quotes str = "This is \"Geeksforgeeks\"." print ( str ) |
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.