Cancel space after Integer'Image value in Ada

别说谁变了你拦得住时间么 提交于 2019-12-04 01:11:31

问题


when I'm print this procedure below -

procedure put (Date:Date_Type) is
begin
  Put(Integer'Image(Date.Day)); --'
  Put("-");
  Put(Integer'Image(Date.Month)); --'
  Put("-");
  Put(Integer'Image(Date.Year)); --'
end;

The result is (for example) : 1- 1- 2010

My question is how to prevent the spacing of one character before every Date value. (day/month/year). Of course I'm using Date procedure with record inside holding day/month/year.

Thanks in advance.


回答1:


You have a few options:

  • If you know the Integer value is always non-negative, you can slice the string to omit the leading blank.
  • You can use the Ada.Strings.Fixed.Trim() function to trim off the blank.
  • You can use the Put() procedure from an Ada.Text_IO.Integer_IO instantiation (such as the preinstantiated Ada.Integer_Text_IO).

Here's some code to illustrate:

with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Strings.Fixed;

procedure Int_Image is

   use Ada.Text_IO;
   use Ada.Integer_Text_IO;
   use Ada.Strings.Fixed;

   N : Integer := 20;

   Raw_Image     : constant String := Integer'Image(N);

   Trimmed_Image : constant String := Trim(Raw_Image, Ada.Strings.Left);

   Sliced_Image  : constant String := Raw_Image(2 .. Raw_Image'Last);

begin
   Put_Line("Raw 'image    :" & Raw_Image & ":");
   Put_Line("Trimmed image :" & Trimmed_Image & ":");
   Put_Line("Sliced image  :" & Sliced_Image & ":");
   Put     ("'Put' image   :");
   Put     (N, Width => 0);
   Put_Line(":");
end Int_Image;

Compiling and running this with GNAT yields:

$./int_image
Raw 'image    : 20:
Trimmed image :20:
Sliced image  :20:
'Put' image   :20:


来源:https://stackoverflow.com/questions/1846737/cancel-space-after-integerimage-value-in-ada

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!