------------------------------------------------------------------------------
-- accessing substrings
s1 : string := data(start..ending);
-- "end" is a reserved word in Ada
s2 : string := data(start..data'Last); -- substrings accessed through array slices
-----------------------------------------------------------------------------
-- different string types
-- Bounded strings have a fixed maximum length and may vary in size up to the
-- maximum length.
package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length(80);
s : Bounded.Bounded_String := Bounded.To_Bounded_String("Hello World!");
-- Unbounded strings have no maximum size and may vary in size and content
use Ada.Strings.Unbounded;
s : Unbounded_String := To_Unbounded_String("Hello World!");
------------------------------------------------------------------------------
-- No pack/unpack equivalents exist in standard Ada libraries.
-- Examples here use a custom implementation to split an original string into
-- chunks of specified length.
-- TO BE DONE
------------------------------------------------------------------------------
-- Aggregates repeating a basic pattern s :
string := 5 * "Wow! ";
-- result: "Wow! Wow! Wow! Wow! Wow! "
------------------------------------------------------------------------------
data : string := "This is what you have";
-- forward 123456789012345678901
-- Any data element can be accessed directly through its index.
-- Substrings can be accessed directly through array slices.
first : character := data(1); -- 'T'
start : string := data(6..7); -- "is"
rest : string := data(14..data'Last); -- "you have"
last : character := data(data'Last); -- 'e'
final : string := data(data'Last - 3..data'Last);-- "have"
piece : string := data(14..16); -- "you"
------------------------------------------------------------------------------
-- String contents can be rearranged as long as the length of the string does
-- not change. If you want to change the length then use either Bounded or
-- Unbounded strings.
data : string := "This is what you have";
data(18..21) := "were"; -- "This is what you were"
data(1..7) := "Is this"; -- "Is this what you were"
------------------------------------------------------------------------------
use GNAT.Regpat;
-- roughly the same set of regular expressions as are available in Perl or
-- Python.
Match_Position : Natural;
Match_Position := Match(Compile("pattern"),data(data'last - 9..data'last));
if Match_Position > 0 then
Put_Line("Pattern matches in last 10 characters");
end if;
-- substitute "at" for "is", restricted to the first five characters
declare
Matches : Match_Array(0..4);
Regexp : string := "is";
Data : string := "This is what you have";
begin
Match(Regexp, Data(1..5), Matches);
if Matches(0).First > 0 then
Data(Matches(0).First .. Matches(0).Last) := "at";
end if;
end;
-- exchange the first and last letters in a string
data : string := "make a hat";
temp : character := data(1);
data(1) := data(data'Last);
data(data'Last) := temp; -- "take a ham"
-- extract a column with array slicing
data : string := "to be or not to be";
a1 : string := data(7..12); -- "or not"
-- forward 6, grab 2, backward 5, grab 2
a2 : string := data(7..8) & data(4..5); -- "orbe"
------------------------------------------------------------------------------
|