Text Strings


πŸ“œ Text can be considered to be a chain of letters. This is why to the simplest unit of text computer scientist decided to call it "strings of characters". Lazy as we are, we call them just "string" most of the time.

πŸ“œ To keep it the simplest possible for all systems, Ada has a built in String type. They are static arrays (can't change length) and are not designed for text processing, but to have a simple interaction with the program, and process input-output in an optimal way.

πŸ“œ All text types will be analogous, so understanding strings becomes basic issue.

 Char : Character := 'A';
 --Characters use single quotes
 S : String := "Strings use double quotes " & '"' & " and can be chained together with " & '"' & '&' & '"';


 S1: String; 
 --  Wrong!  The compiler doesn't know how much space it takes
 S2: String := "This is a message";  
 --  Good. The space is declared and initialized.
 S3: String(1..20);
 --  Good. The space is declared for 20 characters. 
 --  Initialization can be done later.
 ...
 S2 := "This won't work"
 -- 

πŸ“œ Other text types exist in Ada that are more versatile to do text processing (as in word documents), but Strings can be used for most, if not any, application with some creativity to escape the limitations. Nonetheless, you should be using advanced types to make your work easier.

with Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO;
with Ada.Text_IO;

procedure Texts_Types

 S : String := "Strings: Static Text";
 US : Ada.Strings.Unbounded.Unbounded_String;
--  use type ASU.Unbounded String
---- Allows use of operators (& = etc)

begin
US := To_Unbounded_String( 
   "Unbounded Strings: Dynamic text" );
  --  For conversion to Unbounded String
Put( To_String(US) ); 
  --  For conversion to String
US := To_Unbounded_String(S); 
  --  Changed Dynamically

end Texts_Types;