你有几个选择:
- 如果您知道整数值始终为非负数,则可以对字符串进行切片以省略前导空格。
- 您可以使用 Ada.Strings.Fixed.Trim() 函数来修剪空白。
- 您可以使用 Ada.Text_IO.Integer_IO 实例化(例如预实例化的 Ada.Integer_Text_IO)中的 Put() 过程。
这里有一些代码来说明:
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;
使用 GNAT 编译和运行它会产生:
$./int_image
Raw 'image : 20:
Trimmed image :20:
Sliced image :20:
'Put' image :20: