4.7 Strings

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

Every string is an array of components of type Character (or type Wide_Character). The built-in types String and Wide_String are unconstrained arrays (declared in package Standard).

   type String is array (1..<>) of Character;

When a variable (or constant) string is declared it must be initialized, and thus constrained.

   My_String : String := "Hello there";

Ada.Text_IO (and its children) provides some basic string input-output facilities, which have already been illustrated in earlier pages. These facilities include various Get and Put procedures and "&", the concatenation operator.

The predefined environment contains a number of packages that provide many useful facilities for manipulating strings. Some of the more important of these packages are: Ada.Strings and its children (grandchildren of Ada) Ada.Strings.Fixed, Ada.Strings.Bounded and Ada.Strings.Unbounded. It would take many pages to cover all of these facilities. (See Section A.4 of [ARM95].) Some of them are similar to those provided by <strings.h> for C programmers. Some example capabilities are:

In the example program below, we illustrate use of the Move procedure exported by package Ada.Strings.Fixed. This is used to copy a "source" string of one length into a "target" string of a (possibly) different length. If the source is shorter than the target, the target is "padded" with blank characters. If the source is longer than the target, the target is truncated.

Example Program Illustrating Use of  the Move procedure

The package Name_Converter depends upon Ada.Strings and Ada.Strings.Fixed, and exports a Fixed_Name function that uses the Move procedure. The package Home_Run_Records depends upon Name_Conveter and Ada.Text_IO. It exports two procedures and encapsulates a record type and an array of these types.

Im4-8.gif (6924 bytes)

Note the two explicit conversions, one from String to String_11 and one from String_11 to String.

Source Code Listing

----------------------------------------------------------
--------------------  Name_Converter ---------------------
--  This package exports a string type of length 11 and a 
--  function that converts an input string of any length 
--  to one of length 11. The function is implemented using 
--  the Move procedure provided by Ada.Strings.Fixed.
----------------------------------------------------------
package Name_Converter is
  
  type String_11 is array (1..11) of Character;
  function Fixed_Name(Name : String) return String_11;
          
end Name_Converter;
----------------------------------------------------------
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
package body Name_Converter is
    
  Name_11 : String := "           ";
    
  function Fixed_Name(Name : String) return String_11 is
  begin
    Move(Source => Name,        -- Move (truncate or pad as needed)
         Target => Name_11,
         Drop   => Right); 
    return String_11(Name_11);  -- type conversion        
  end Fixed_Name;
  
end Name_Converter;
----------------------------------------------------------
-------------------- Home_Run_Records --------------------
--  This package exports two procedures. One fills in an 
--  array of records containing names and home run data. 
--  The other displays all the data in the array. Player 
--  name strings are converted using the Fixed_Name 
--  function of package Name_Converter. 
----------------------------------------------------------
package Home_Run_Records is    
  procedure Fill_Records;
  procedure Display_Records;
end Home_Run_Records;
----------------------------------------------------------
with Name_Converter;
with Ada.Text_IO; use Ada.Text_IO;
package body Home_Run_Records is
   
  type Home_Run_Record_Type is
    record
      Name      : Name_Converter.String_11;
      Year      : Integer range 1900..2099;
      Home_Runs : Integer range 0..99;
    end record;
    
  HR : array (1..4) of Home_Run_Record_Type;
  ------------------------------------------
  procedure Fill_Records is
  begin
    HR(1).Name := Name_Converter.Fixed_Name("Ruth, Babe");
    HR(2).Name := Name_Converter.Fixed_Name("Maris, Roger");
    HR(3).Name := Name_Converter.Fixed_Name("Sosa, Sammy");
    HR(4).Name := Name_Converter.Fixed_Name("McGuire, Mark");
    HR(1).Year := 1927; HR(1).Home_Runs := 60;
    HR(2).Year := 1961; HR(2).Home_Runs := 61;
    HR(3).Year := 1998; HR(3).Home_Runs := 66;
    HR(4).Year := 1998; HR(4).Home_Runs := 70;
  end Fill_Records;
  ------------------------------------------
  procedure Display_Records is
  begin
    
    Put_Line("   Name       Year   HR");
    New_Line;  
      
    for I in 1..4 loop
      Put(String(HR(I).Name) & "  ");    -- type conversion
      Put(Integer'Image(HR(I).Year) & "  " &
          Integer'Image(HR(I).Home_Runs));
      New_Line; 
    end loop;
  end Display_Records;
  ------------------------------------------ 
end Home_Run_Records;
----------------------------------------------------------
------------------------ Test_Strings --------------------
--  This test procedure calls the two procedures exported 
--  by package Home_Run_Records.
----------------------------------------------------------
with Home_Run_Records;
procedure Test_Strings is
begin
  Home_Run_Records.Fill_Records;
  Home_Run_Records.Display_Records;  
end Test_Strings;
----------------------------------------------------------

The above program produces the following output:

        Name       Year   HR

     Ruth, Babe    1927   60
     Maris, Roge   1961   61
     Sosa, Sammy   1998   66
     McGuire, Ma   1998   70

Note that "Ruth, Babe" was padded with an extra blank, "Sosa, Sammy" was unchanged, and the other two original strings were truncated.

Related Topics

4.1 Type System Overview 4.6 Array Types
B.2 Package Ada and Children

[ Back to top of pagePrev ] Next ]