Section 8.3 - Basics of Type String

Ada's type String can be considered a "primitive" type for handling sequences of text. It's simple and efficient, but some operations require a little work.

A String is simply an array of characters. The major "catch" with variables of type String is that, when you create a String variable, you must give Ada a way of determining its length - and the variable will stay fixed at that length from then on. There are two major ways for determining a string's length - you can explicitly state how long the string will be, or you can assign the variable a string value (Ada will determine how long the string value is and make the new variable that length).

Ada requires that at least the bounds or the initial string value be given; if both are given, they must match. The "low_bound" is usually 1, though Ada permits the low_bound to be a larger Integer. Here are some examples:

A        : String(1..50);   -- Variable A holds 50 characters.
Question : String := "What is your name?"  -- Ada will make String(1..18).

Here's a simplified BNF for declaring String variables:

declare_string_variable ::= list_of_variable_names ":" [ "constant" ]
                           "String" [ bounds ] 
                                    [ ":=" initial_string_value ]
bounds ::= "(" low_bound ".." high_bound ")"
Once you have a string, you can use predefined Ada operations on arrays (since a string is simply an array of characters). These include the following:
  1. You can read or overwrite a Character (an element of a String) at a given index position. For example, A(2) refers to the character in string A at index position 2. Any attempt to read or write a nonexistent index position will cause the exception Constraint_Error to be raised. To change the character at a given position, simply assign to it, for example:
        A(2) := 'f';
  2. You can read or overwrite a slice (i.e., a substring). A slice refers to a portion of a string, from one index position to another, and is also considered a String. A slice from index position "low" to position "high" of some String variable B is written as "B(low..high)". You can write to a slice, too, but the source and destinations must have the same length.
  3. You can assign a whole string from one String to another the same way as any other variable, as long as their lengths are equal, like this:
        B := A;
  4. You can concatenate (combine) strings together using the "&" operator.

There are also predefined operations in Text_IO for printing Strings, namely Put and Put_Line. Let's look at an example:

  with Text_IO; use Text_IO;
  procedure String1 is
    A : String := "Hello";
    B : String(1..5);
    B := A;                      -- B becomes "Hello"
    A(1) := 'h';                 -- A becomes "hello"
    A(2..3) := A(4..5);          -- A becomes "hlolo"
    A := B(1) & A(2..3) & "ol";  -- A becomes "Hlool"
    A(2..3) := B(2..3);
  end String1;


What is the last line that program String1 will print?

  1. Hello
  2. Hlolo
  3. hello
  4. Helol

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 8 outline

David A. Wheeler (

The master copy of this file is at "".