Ada string concatenation

I have a function that returns a string for a specific element, and I need to call this function many times and combine these strings into one. The concatenated string is limited. I had to fill it in when there are space characters, when it is initialized, but I keep getting length check errors. Is there anything basic I'm doing wrong here?

FOR I IN 1..Collection.Size LOOP  
    Combined_String :=  combined_string & Tostring(Collection.Book(I));  
END LOOP;
+3
source share
5 answers

Unbounded_String is probably the easiest way:

with Ada.Strings.Unbounded;
use Ada.Strings.unbounded;

   ...

Temp_Unbounded_String : Unbounded_String;  -- Is empty by default.

   ...

for I in 1 .. Collection.Size loop
   Append(Temp_Unbounded_String, ToString(Collection.Book(I));
end loop;

If you need the result to be placed on a standard fixed-length string:

declare
   Temp_String : constant String := To_String(Temp_Unbounded_String);
begin
   -- Beware! If the length of the Temp_String is greater than that of the
   -- fixed-length string, a Constraint_Error will be raised.  Some verification
   -- of source and target string lengths must be performed!
   Combined_String(Temp_String'Range) := Temp_String;
end;

Alternatively, you can use the Ada.Strings.Fixed Move () procedure to cast an Unbounded_String to a fixed-length string:

Ada.Strings.Fixed.Move(To_String(Temp_Unbounded_String), Combined_String);

, " ", Length_Error. Move(), , . .

+5

Combined_String, . "" Ada.

, , Ada.Strings.Unbounded - , , , .

+3

, , , 2012 , , , , ...

declare
  function Concatenate(i: Collection'index)
  is
    (tostring(Collection(i) &
      if (i = Collection'last) then
        ("")
      else
        (Concatenate(i+1))
    );

  s: string := Concatenate(Collection'first);
begin
  Put_Line(s);
end;

, ; , , ( ).

Ada 2012 !

+2

Ada , . 99% , , - .

, , .

, , . -, "" ( ) Ada.Strings.Unbounded, Dave Marc C.

( , ) . :

function Combined_String (String_Collection : in String_Collection_Type) return String is
begin
    if String_Collection'length = 1 then 
        return String_Collection(String_Collection'first);
    end if;    
    return String_Collection(String_Collection'first) & 
           Combined_String (String_Collection'first + 1 .. String_Collection'last);
end Combined_String;

, Collection, . , . , , , , .

+1

AdaPower.com:

function Next_Line(File : in Ada.Text_IO.File_Type :=
   Ada.Text_Io.Standard_Input) return String is
   Answer : String(1..256);
   Last   : Natural;
begin
   Ada.Text_IO.Get_Line(File => File,
      Item => Answer,
      Last => Last);
   if Last = Answer'Last then
      return Answer & Next_Line(File);
   else
      return Answer(1..Last);
   end if;
end Next_Line;

, ( Get_Line) , . , , , - :

function Combined_String (String_Collection : in String_Collection_Type)
                   Return String is      
begin
    if String_Collection'length = 1 then 
        Return String_Collection(String_Collection'First).All;
    end if;

      Recursion:
      Declare
         Data : String:= String_Collection(String_Collection'First).All;
         SubType Constraint is Positive Range
           Positive'Succ(String_Collection'First)..String_Collection'Last;
      Begin
         Return Data & Combined_String( String_Collection(Constraint'Range) );
      End Recursion;
end Combined_String;

, String_Collection :

 Type String_Collection is Array (Positive Range <>) of Access String;

* Integer'Range, IIRC

+1

Source: https://habr.com/ru/post/1791908/


All Articles