Skip to content

Commit 22fc67e

Browse files
authored
Merge pull request #1209 from gusthoff/content/advanced_ada/new_content/strings/character_string_literals/20250406
Adding section on character and string literals
2 parents c208c3f + 88a1bfb commit 22fc67e

File tree

1 file changed

+140
-0
lines changed
  • content/courses/advanced-ada/parts/data_types

1 file changed

+140
-0
lines changed

content/courses/advanced-ada/parts/data_types/strings.rst

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,146 @@ Strings
33

44
.. include:: ../../../global.txt
55

6+
.. _Adv_Ada_Character_String_Literals:
7+
8+
Character and String Literals
9+
-----------------------------
10+
11+
So far, we're already seen many examples of string literals |mdash| both in
12+
the :ref:`Introduction to Ada <Intro_Ada_Course_Index>` course and in the
13+
present course. In this section, we define them once more and discuss a couple
14+
of details about them.
15+
16+
17+
Character Literals
18+
~~~~~~~~~~~~~~~~~~
19+
20+
A character literal is simply a character between apostrophes (or
21+
*single quotation marks*). For example:
22+
23+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Strings.Character_String_Literals.Character_Literals
24+
25+
with Ada.Text_IO; use Ada.Text_IO;
26+
27+
procedure Show_Character_Literals is
28+
C : Character := 'a';
29+
-- ^^^
30+
-- Character literal
31+
begin
32+
Put_Line ("Character : " & C);
33+
end Show_Character_Literals;
34+
35+
In this example, we initialize the character variable :ada:`C` with the
36+
character literal :ada:`'a'`.
37+
38+
39+
String Literals
40+
~~~~~~~~~~~~~~~
41+
42+
A string literal is simply a collection of characters between quotation marks.
43+
For example:
44+
45+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Strings.Character_String_Literals.Simple_String_Literals
46+
47+
with Ada.Text_IO; use Ada.Text_IO;
48+
49+
procedure Show_Simple_String_Literals is
50+
S1 : String := "Hello";
51+
-- ^^^^^^^
52+
-- String literal
53+
54+
S2 : String := "World";
55+
-- ^^^^^^^
56+
-- String literal
57+
begin
58+
Put_Line (S1 & " " & S2);
59+
end Show_Simple_String_Literals;
60+
61+
In this example, :ada:`"Hello"` and :ada:`"World"` are string literals.
62+
63+
64+
String literals with quotation
65+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66+
67+
If you want to include a quotation mark in a string literal, you have to write
68+
:ada:`""` (inside that string literal):
69+
70+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Strings.Character_String_Literals.String_Literals_With_Quotes
71+
72+
with Ada.Text_IO; use Ada.Text_IO;
73+
74+
procedure Show_String_Literals_With_Quotes is
75+
S1 : String := "Hello";
76+
S2 : String := "World";
77+
begin
78+
Put_Line (" "" " & S1
79+
-- ^^
80+
-- Quotation marks
81+
& " " & S2 & " "" ");
82+
-- ^^
83+
-- Quotation marks
84+
85+
Put_Line ("""Hello World!""");
86+
-- ^^ ^^
87+
-- Quotation marks
88+
89+
Put_Line ("""""");
90+
-- ^^^^
91+
-- Quotation marks
92+
end Show_String_Literals_With_Quotes;
93+
94+
In this example, we display ``" Hello World "`` to the user by adding
95+
quotation marks to the concatenated strings in the call to :ada:`Put_Line`.
96+
97+
Note that the three quotation marks at the beginning of
98+
:ada:`"""Hello World!"""` consist of the quotation mark that indicate the
99+
beginning of the string literal and the two quotation marks that represent a
100+
single quotation mark inside the string literal. (The same thing happens at the
101+
end of this string literal, but in reverse.) This string literal is displayed
102+
as ``"Hello World!"`` to the user.
103+
104+
Finally, the string literal :ada:`""""""` is displayed as ``""`` to the
105+
user.
106+
107+
108+
Empty string literals
109+
^^^^^^^^^^^^^^^^^^^^^
110+
111+
An empty string is represented by quotation marks without characters in
112+
between: :ada:`""`. For example:
113+
114+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Strings.Character_String_Literals.Empty_String_Literals
115+
116+
with Ada.Text_IO; use Ada.Text_IO;
117+
118+
procedure Show_Empty_String_Literals is
119+
S1 : String := "";
120+
S2 : String (1 .. 0) := "";
121+
begin
122+
Put_Line (S1);
123+
Put_Line (S2);
124+
Put_Line ("");
125+
end Show_Empty_String_Literals;
126+
127+
Note that an empty string is an array of characters without any components.
128+
This is made explicit by the declaration of :ada:`S2`. Here, by using the range
129+
:ada:`1 .. 0`, we're declaring an empty array.
130+
131+
.. todo::
132+
133+
Add link to subsection on empty arrays.
134+
135+
.. admonition:: In other languages
136+
137+
In C, an empty string still contains a single character: the null character
138+
(``\0``). In Ada, however, an empty string doesn't have any characters.
139+
140+
.. admonition:: In the Ada Reference Manual
141+
142+
- :arm22:`2.5 Character Literals <2-5>`
143+
- :arm22:`2.6 String Literals <2-6>`
144+
145+
6146
.. _Adv_Ada_Wide_Wide_Strings:
7147

8148
Wide and Wide-Wide Strings

0 commit comments

Comments
 (0)