Ada unbounded and bounded strings
last modified November 23, 2026
A fixed String keeps the length it was given, so joining two words
into an existing variable fails when the result is longer. Ada offers two
library types for text that changes size. Unbounded_String grows
without a declared limit, and a bounded string has a maximum length chosen when
the program instantiates a generic package. Both are private types with
operations for converting to and from String, appending, searching,
and editing. This article uses them in turn.
Strings that grow
Ada.Strings.Unbounded declares the private type
Unbounded_String and the conversions
To_Unbounded_String and To_String. The type manages
its own storage, so an assignment may replace a short value with a longer one.
The length is available from Length, and the empty value is
returned by Null_Unbounded_String.
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
with Ada.Text_IO;
use Ada.Text_IO;
procedure Grow is
Name : Unbounded_String := To_Unbounded_String ("Ada");
begin
Put_Line (To_String (Name));
Name := Name & " 2022";
Put_Line (To_String (Name));
Put_Line ("length =" & Natural'Image (Length (Name)));
end Grow;
The variable starts with three characters and ends with eight. The
& operator for this type comes from the same package, which is
why the use clause is present; without it the operator would not be
directly visible and the call would need the package name. Printing needs
To_String, because Put_Line accepts a fixed
String.
$ gnatmake -gnat2022 -gnatwa grow.adb $ ./grow Ada Ada 2022 length = 8
Appending pieces
Append adds text to the end of an existing unbounded string and
changes it in place, so it takes the string as an in out
parameter. It has overloads for a String and for a single
Character, which makes it convenient for building a line one piece
at a time. Starting from Null_Unbounded_String produces an empty
value with no characters.
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Text_IO; use Ada.Text_IO; procedure Append_Demo is Message : Unbounded_String := Null_Unbounded_String; begin Append (Message, "Hello"); Append (Message, ", "); Append (Message, "there"); Append (Message, '!'); Put_Line (To_String (Message)); end Append_Demo;
Four calls build Hello, there! from an empty value. The first three
append strings, including one that contains only a comma and a space, and the
last appends a character literal. Each call may allocate a larger buffer
internally, but the program only works with the same
Unbounded_String variable.
$ gnatmake -gnat2022 -gnatwa append_demo.adb $ ./append_demo Hello, there!
Index and Slice
The unbounded package repeats the useful operations of
Ada.Strings.Fixed for its own type. Index returns the
position of a pattern or 0 when it is absent, and Slice returns a
String containing the characters from a low to a high position.
The positions are counted from 1, as with a fixed string.
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
with Ada.Text_IO;
use Ada.Text_IO;
procedure Slice_Index is
Text : constant Unbounded_String :=
To_Unbounded_String ("the quick brown fox");
Pos : constant Natural := Index (Text, "brown");
begin
Put_Line ("found at" & Natural'Image (Pos));
Put_Line (Slice (Text, Pos, Pos + 4));
Put_Line (Slice (Text, 1, 3));
end Slice_Index;
brown starts at position 11, so Pos + 4 is 15 and the
second slice returns exactly the word. The third call ignores the search result
and takes the first three characters, the. If the pattern were
missing, Pos would be 0 and the slice would be rejected at run
time because its bounds would be invalid.
$ gnatmake -gnat2022 -gnatwa slice_index.adb $ ./slice_index found at 11 brown the
Insert, replace, and delete
Three procedures edit an unbounded string in place.
Insert puts new text before a given position,
Replace_Slice replaces a range of positions with new text, and
Delete removes a range. The positions refer to the current value,
so each edit changes the positions used by the next one.
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
with Ada.Text_IO;
use Ada.Text_IO;
procedure Insert_Delete is
Text : Unbounded_String := To_Unbounded_String ("the fox");
begin
Insert (Text, 5, "quick ");
Put_Line (To_String (Text));
Replace_Slice (Text, 5, 9, "slow");
Put_Line (To_String (Text));
Delete (Text, 1, 4);
Put_Line (To_String (Text));
end Insert_Delete;
The first edit inserts quick before position 5, which is the
f of fox, producing the quick fox. The
second replaces positions 5 to 9, the five letters of quick, with
slow. The last removes positions 1 to 4, the word
the and its space, leaving slow fox.
$ gnatmake -gnat2022 -gnatwa insert_delete.adb $ ./insert_delete the quick fox the slow fox slow fox
Bounded strings
An unbounded string may allocate at any time, which is not always acceptable.
Ada.Strings.Bounded provides a generic package whose parameter
Max sets the capacity once. The program instantiates the generic
with the wanted maximum and gets a type Bounded_String, operations
such as Append, and the constant Max_Length. Storage
is reserved for the maximum, so the value never allocates after the
instantiation. The new form creates a package from a template; the
generics article later in the series explains how such templates are written,
and for now the line can be read as "make a bounded-string package with a
maximum of 12".
with Ada.Strings;
with Ada.Strings.Bounded;
with Ada.Text_IO;
use Ada.Text_IO;
procedure Bounded_Demo is
package Short_Strings is
new Ada.Strings.Bounded.Generic_Bounded_Length (Max => 12);
use Short_Strings;
Name : Bounded_String := To_Bounded_String ("Ada");
begin
Append (Name, " Lovelace");
Put_Line (To_String (Name));
Put_Line ("length =" & Natural'Image (Length (Name)));
if Length (Name) = Max_Length then
Put_Line ("the buffer is full");
end if;
Name := To_Bounded_String ("Ada Lovelace!", Drop => Ada.Strings.Right);
Put_Line (To_String (Name));
end Bounded_Demo;
Ada Lovelace has exactly twelve characters, so the value fills the
buffer and the test against Max_Length succeeds. The last
assignment converts a thirteen-character literal, and
Drop => Ada.Strings.Right discards the character beyond the
limit, leaving the twelve that fit. Instantiating the generic for another
maximum produces a distinct type, so bounded strings of different capacities
cannot be mixed by accident.
$ gnatmake -gnat2022 -gnatwa bounded_demo.adb $ ./bounded_demo Ada Lovelace length = 12 the buffer is full Ada Lovelace
Overflowing a bounded string
The Drop parameter defaults to Error, which means that
text which does not fit raises Length_Error instead of being
silently cut. The next program appends one character to a full buffer without
asking for truncation. Handling the error is the subject of the exceptions
article later in the series; here the program simply stops.
with Ada.Strings.Bounded;
with Ada.Text_IO;
use Ada.Text_IO;
procedure Overflow_Bounded is
package Short_Strings is
new Ada.Strings.Bounded.Generic_Bounded_Length (Max => 12);
use Short_Strings;
Name : Bounded_String := To_Bounded_String ("Ada Lovelace");
begin
Append (Name, '!');
Put_Line (To_String (Name));
end Overflow_Bounded;
The buffer already holds twelve characters, so appending the exclamation mark cannot succeed. The program writes the unhandled exception to the standard error stream and exits with a non-zero status. The file and line in the message belong to the implementation of the bounded-string package, not to the example, so they may differ between GNAT versions.
$ gnatmake -gnat2022 -gnatwa overflow_bounded.adb $ ./overflow_bounded raised ADA.STRINGS.LENGTH_ERROR : a-strsup.adb:678
An applied example: splitting a line
The final program splits a fixed string into words and stores them in an unbounded string. It searches for a space in the part that has not been processed yet, takes the slice before the space, and moves the start past it. When no space remains, the rest of the line is the last word.
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
with Ada.Text_IO;
use Ada.Text_IO;
procedure Split_Words is
Line : constant String := "red green blue";
Result : Unbounded_String := Null_Unbounded_String;
Start : Positive := Line'First;
Pos : Natural;
begin
loop
Pos := Ada.Strings.Fixed.Index (Line (Start .. Line'Last), " ");
if Pos = 0 then
Append (Result, "[" & Line (Start .. Line'Last) & "]");
exit;
end if;
Append (Result, "[" & Line (Start .. Pos - 1) & "]");
Start := Pos + 1;
end loop;
Put_Line (To_String (Result));
end Split_Words;
The slice Line (Start .. Line'Last) keeps the original bounds, so
Index returns a position in Line rather than a
position counted from the start of the slice. That is why
Line (Start .. Pos - 1) is the word before the space. The loop
exits after appending the final word, and the unbounded result grows to hold
all three bracketed words.
$ gnatmake -gnat2022 -gnatwa split_words.adb $ ./split_words [red][green][blue]
Next steps
The next article, arrays, generalises the idea behind
String: an indexed collection whose bounds may be fixed or
supplied by an initial value. It also explains the slice notation used here in
detail, together with aggregates, unconstrained array types, and
multidimensional arrays.
Source
In this article we have grown an Unbounded_String, appended
strings and characters, searched and sliced it, inserted, replaced, and deleted
text, instantiated a bounded string package with a maximum length, seen
Length_Error when a buffer overflows, and split a line into
unbounded pieces. The previous article covers the
fixed strings that these types convert to and from.
Author
List all Ada tutorials.