在Ada中的Integer'Image值后取消空间 [英] Cancel space after Integer'Image value in Ada

查看:125
本文介绍了在Ada中的Integer'Image值后取消空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在下面打印此过程时-

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;

结果是(例如):1- 1- 2010

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

我的问题是如何防止每个Date值前一个字符的间距。 (日月年)。
当然,我使用的是Date过程,其中记录在持有日/月/年内。

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.

预先感谢。

推荐答案

您有几个选择:


  • 如果您知道Integer值始终为非负数,您可以对字符串进行切片以省略前导空格。

  • 您可以使用Ada.Strings.Fixed.Trim()函数来修剪空格。
  • li>
  • 您可以从Ada.Text_IO.Integer_IO实例(例如预先实例化的Ada.Integer_Text_IO)中使用Put()过程。

  • 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;

使用GNAT编译并运行它会产生以下结果:

Compiling and running this with GNAT yields:

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

这篇关于在Ada中的Integer'Image值后取消空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆