Characters and strings
Characters and strings are the basic data types used to manipulate text. In Julia, characters are individual Unicode characters, and strings are sequences of characters. These data types are often used for text processing, message output, and data manipulation. If you work with text data, such as analysing logs, processing user input or generating reports, then understanding how to work with characters and strings will be extremely useful.
Creating a character and string
Single quotes are used to create a symbol. Symbols are useful when you need to work with individual characters, such as when processing text:
ch = 'x'
typeof(ch)
Output
Char
To enter a string, use double quotes. Strings allow you to store and process text data:
str = "That's string"
Output
"That's string"
If the text in a string contains inverted commas, the string is enclosed in three double inverted commas. This is useful for working with text that includes inverted commas or other special characters:
quotedstr = """That's "quoted" string"""
print(quotedstr)
Classic Julia output via REPL[1] will display a string with a backslash \ - "That’s a \"quoted\" string" . To see the string without slashes, use the print function.
|
Output
That's "quoted" string
String indexing
Indexing allows you to extract individual characters or substrings from a string. This is useful for analysing text or extracting relevant data. In Julia, indexing starts at 1, which may be unusual for those who have worked with languages where indexing starts at 0.
Example of character extraction:
str[6]
Output
's': ASCII/Unicode U+0073 (category Ll: Letter, lowercase)
Example of substring extraction:
str[10:13]
Output
"ring"
For convenience, you can use the begin
and end
keywords to refer to the beginning and end of a line:
str[end-1]
Output
'n': ASCII/Unicode U+006E (category Ll: Letter, lowercase)
concatenation
Concatenation is combining strings or characters into a single string. It is useful for creating complex text data. In Julia, you can use the string
function or the *
operator for concatenation.
An example of concatenation:
a = 'А'
b = 'Б'
joke = "сидели на трубе"
string(a, " и ", b, ' ', joke)
Output
"А и Б сидели на трубе"
Useful tips
-
Use the correct quotation marks - remember that characters are created using single quotes (') and strings are created using double quotes (""). Use triple double quotes (""") for strings with inverted commas inside.
-
Begin indexing - string indexing starts at 1, not 0 as in some other languages. Use
begin
andend
for easy access to the beginning and end of a string. -
Experiment with concatenation - try concatenating strings and characters in different ways to see how it works. Read more in the article Arrays, vectors and matrices in Engee.
-
Use built-in functions - Julia provides many functions for working with strings, such as
length
,split
,replace
and others. Explore them to simplify text processing.Example of working with functions for strings
An example of usage of the
length
function to get the length of a string:text = "Hello, Julia!" println("Длина строки: ", length(text))
Example usage of the
plit
function to split a string into parts:words = split(text, " ") println("Разделенные слова: ", words)
Example usage of the
replace
function to replace part of a string:new_text = replace(text, "Julia" => "World") println("Новая строка: ", new_text)
Output:
Длина строки: 13 Разделенные слова: ["Hello,", "Julia!"] Новая строка: Hello, World!