Previous topic | Next topic | Ada Home Page | Index

STRING type

A STRING is used when you want to represent a sequence of characters as a single unit of data.

Examples are:

String data type

declaration:
    max_str_length : constant := 26;
    alphabet, response  : STRING (1 .. max_str_length);

literals:

STRING Operations

Assignment:

        alphabet := "abcdefghijklmnopqrstuvwxyz";

        response := alphabet;

Comparisons:

Concatenation (&)

   alphabet(1..3) & alphabet(26..26)        = "abcz"

   PUT("The alphabet is " & alphabet & '.');

Sub-strings

individual character: specify position

    alphabet(10)      'j'
    alphabet(17)      'q'

slice: specify range of positions

    alphabet(20..23)  "tuvw"
    alphabet(4..9)    "defghi"

assign to compatible slice

    response(1..4) := "FRED";
    response          "FREDefghijklmnopqrstuvwxyz"

slice with equal bounds: STRING of one character

    response(1..1)    "F"  (not the same as 'F')

An example program shows the use of slices to manipulate parts of strings.

A second example shows how the same thing can be achieved using both slices and concatenation.

String I/O

TEXT_IO

Output: PUT, PUT_LINE

GET:

    GET(a_string);

GET_LINE

    GET_LINE(a_string, n);

An example program shows the use of GET for numbers and GET_LINE for strings.


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 12 Feb 96