Section 8.5 - Unbounded_String Basics
The Unbounded_String type is defined in the package
Ada.Strings.Unbounded, so you'll need to ``with'' package
Ada.Strings.Unbounded to use the Unbounded_String type.
Package Ada.Strings.Unbounded also provides a number
of useful basic operations on Unbounded_String.
The
Ada RM section A.4.5
provides a complete definition of package Ada.Strings.Unbounded.
Here are a few important operations:
-
Function To_Unbounded_String takes something of type String and converts
it into type Unbounded_String.
This is useful for a variety of purposes, for example, for
setting Unbounded_String values to some constant value,
as we'll show later.
-
Function To_String is the reverse; it
takes something of type Unbounded_String and converts it to type String.
-
Function Length takes an Unbounded_String and returns
the number of characters currently stored in the Unbounded_String.
-
Procedure Append takes two arguments; it appends to the end of
its first argument the contents of the second argument.
The first argument has to be an Unbounded_String;
the second argument can be a String or an Unbounded_String.
-
Function Element extracts from a given Unbounded_String (its Source)
the character at a given position (its index).
The leftmost character has an index of 1, just like Pascal;
C and C++ start their indexes at zero.
Here's the official definition of Element:
function Element (Source : in Unbounded_String; Index : in Positive)
return Character;
-
Procedure Replace_Element is the opposite of Element; it lets you
modify a character at a given position.
Here's the official definition of Replace_Element:
procedure Replace_Element (Source : in out Unbounded_String;
Index : in Positive;
By : in Character);
-
Function Slice takes a given Unbounded_String and returns a ``slice''
of it, i.e., all the characters between a given low and high index.
Slice returns a String, so if you want to use its result as an
Unbounded_String, use To_Unbounded_String on the result.
function Slice (Source : in Unbounded_String;
Low : in Positive;
High : in Natural) return String;
-
Procedure Insert takes a New Item (of type String) and inserts it
into an Unbounded_String before a given index.
If the string isn't empty, this will change the Unbounded_String's length.
This procedure's definition is:
procedure Insert (Source : in out Unbounded_String;
Before : in Positive;
New_Item : in String);
-
Procedure Delete takes an Unbounded_String and two indexes, and deletes
the characters between those two index positions (including the end points).
Its definition is:
Procedure Delete
procedure Delete (Source : in out Unbounded_String;
From : in Positive;
Through : in Natural);
Comparison operations (such as "=" and "<") are defined in this package.
There are other routines to modify or search Unbounded_Strings, including
"&" (concatenate two Unbounded_Strings together),
Translate, Trim, Head, Tail, Index, and Find_Token.
You can use assignment (:=) as well to assign one value to another.
Unlike type String, the lengths of the Unbounded_String's need not be equal.
Quiz:
Given a variable ``Input'' of type Unbounded_String,
what expression would return the value of the fourth character in Input?
- Element(Input, 4)
- Replace_Element(Input, 4, 'L')
- Element(4, Input)
You may also:
David A. Wheeler (dwheeler@ida.org)
The master copy of this file is at
"http://www.adahome.com/Tutorials/Lovelace/s8s5.htm".