8.0 Unbounded Strings - JulTob/Ada GitHub Wiki
Ada.Strings.Unbounded
Overview of The Ada.Strings.Unbounded
package defines an unbounded string type called Unbounded_String
. This type is implemented using access types (similar to pointers in other languages) and dynamically allocated memory, which means the strings can grow or shrink as needed without the programmer having to deal with the low-level memory management tasks.
For general purpuse text, especially where strings can be manipulated as necessary, this is the type that should be used.
This works best in a runtime environment such as a desktop or when resources are plentiful and exceeding your character buffer will not result in a catastrophic crash of the application. In embedded systems, this string type should never be used.
Key Features and Functions
To_Unbounded_String
Converts regular Ada strings or string literals to Unbounded_String
.
Assign
Copies the content of one Unbounded_String
to another.
Append
Concatenates one Unbounded_String
to another or appends a regular string or character.
Insert
Inserts a string at a specified position within an Unbounded_String
.
Delete
Removes a portion of the string starting at a given index for a specified length.
Length
Returns the length of the Unbounded_String
.
Is_Empty
Checks if the Unbounded_String
is empty.
To_String
Converts an Unbounded_String
back to a regular Ada string.
Element
Accesses a character at a specific position.
Examples
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure Test_Unbounded_Strings is
U1, U2 : Unbounded_String;
begin
-- Initialize unbounded strings from literals
U1 := To_Unbounded_String("Hello");
U2 := To_Unbounded_String(" World!");
-- Append U2 to U1
Append(U1, U2);
-- Print the concatenated result
Put_Line("Concatenated String: " & To_String(U1));
-- Insert a string
Insert(Source => To_Unbounded_String("Big "), Target => U1, Position => 1);
-- Print the modified string
Put_Line("Modified String: " & To_String(U1));
-- Show length of the string
Put_Line("Length of String: " & Integer'Image(Length(U1)));
end Test_Unbounded_Strings;
with Ada.Text_IO.Unbounded_IO;
with Ada.Strings.Unbounded;
procedure Unbounded_Strings is
US : Ada.Strings.Unbounded.Unbounded_String
:= Ada.Strings.Unbounded.To_Unbounded_String("Hello, ");
US2 : Ada.Strings.Unbounded.Unbounded_String
:= Ada.Strings.Unbounded.To_Unbounded_String("World!");
US_NULL : Ada.Strings.Unbounded.Unbounded_String
:= Ada.Strings.Unbounded.Null_Unbounded_String;
begin
Ada.Text_IO.
Put_Line(
Ada.Strings.Unbounded.
To_String(
US));
Ada.Strings.Unbounded.
Append(US, US2);
Ada.Strings.Unbounded.
Append(Temp1, " From Ada!");
-- Len
Ada.Text_IO.Put_Line("Temp1 length: " &
Natural'Image(Ada.Strings.Unbounded.Length(Temp1)));
end Unbounded_Strings;
Name : Unbounded_String; -- declares a string variable
with Ada.Strings.Unbounded;
with Ada.Text_IO;
procedure Print_Name_List is
use Ada.Strings.Unbounded;
use Ada.Text_IO;
type Department is (HR, Sales, Development);
Name_List : array (Department, 1 .. 3)
of Ada.Strings.Unbounded.Unbounded_String;
begin
Name_List :=
(
( To_Unbounded_String("John"),
To_Unbounded_String("Michel"),
To_Unbounded_String("Mathew")
),
( To_Unbounded_String("Bob"),
To_Unbounded_String("Jacob"),
To_Unbounded_String("Heiko")
),
( To_Unbounded_String("Mery"),
To_Unbounded_String("Jane"),
To_Unbounded_String("Heiko")
));
for dept in Name_List'Range(1) loop
for who in Name_List'Range(2) loop
Put(To_String(Name_List(dept, who)) & " ");
end loop;
New_Line;
end loop;
end Print_Name_List;
-- function Get_Line[(File : in File_Type)] return Unbounded_String;
-- procedure Get_Line ([File : in File_Type; ]Item : out Unbounded_String);
----------------------
with Ada.Strings.Unbounded, Ada.Text_IO.Unbounded_IO;
...
S1, S2: Ada.Strings.Unbounded.Unbounded_String;
...
S1 := Ada.Text_IO.Unbounded_IO.Get_Line;
Ada.Text_IO.Unbounded_IO.Get_Line (S2);
-----------------------
with Ada.Strings.Unbounded, Ada.Text_IO, Ada.Text_IO.Unbounded_IO;
...
S1, S2: Ada.Strings.Unbounded.Unbounded_String;
F : Text_IO.File_Type;
...
S1 := Ada.Text_IO.Unbounded_IO.Get_Line (F);
Ada.Text_IO.Unbounded_IO.Get_Line (F, S2);
-- procedure Put([File : in File_Type;] Item : in Unbounded_String);
-----------------
with Ada.Strings.Unbounded, Ada.Text_IO.Unbounded_IO;
...
S1 : Unbounded_String;
...
Put (S1);
------------------
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
with Text_IO; use Text_IO;
...
S1 : Unbounded_String;
F : File_Type;
...
Put (F, S1);
Put Line
procedure Put_Line ([File : in File_Type; ]Item : in Unbounded_String);
with Ada.Strings.Unbounded, Ada.Text_IO.Unbounded_IO;
use Ada.Strings.Unbounded, Ada.Text_IO.Unbounded_IO;
...
S1 : Unbounded_String;
...
Put_Line (S1);
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
with Text_IO; use Text_IO;
...
S1 : Unbounded_String;
F : File_Type;
...
Put_Line (F, S1);