访问 Ada 中字符串的第一个元素

发布于 2024-10-08 20:12:58 字数 1252 浏览 5 评论 0原文

我有一个字符串传递到一个函数中,我想将字符串的第一个字符与数字进行比较。

I.E. 
if String(1) = "3"  then

当我编译时,我得到:

warning: index for String may assume lower bound of 1
warning: suggested replacement String'First + 1

我真的很想让这个正确,但是当我尝试“首先”时,它实际上获取一个数字,而不是字符。

有更好的方法吗?

我尝试查找“第一个概念”,下面的网站解释说我实际上得到的是索引号,而不是实际内容: http://en.wikibooks.org/wiki/Ada_Programming/Types/array

例如,

Hello_World  : constant String := "Hello World!";
World        : constant String := Hello_World (7 .. 11);
Empty_String : constant String := "";

使用“首先我会得到:

Array    'First 'Last 'Length 'Range
Hello_World   1       12       12     1 .. 12
World         7       11        5     7 .. 11
Empty_String  1        0        0     1 .. 0

基于该信息,我不能从 Hello world 获取 H (进行比较,例如 if Hello_World(1) = "H" then)

编辑: 所以我最初的做法是 (在这种情况下插入一些变量名而不是字符串)

String(String'First .. String'First) = "1"

因此,从我所知道的情况来看,这是可行的,但是,我发现,我没有写出所有这些,而是

String(String'First) = '1'

​​做了同样的事情,但使用了字符比较,这更有意义!

感谢大家的所有回答!

I have a string passed into a function, I would like to compare the first character of the string against a number.

I.E. 
if String(1) = "3"  then

When I compile I get:

warning: index for String may assume lower bound of 1
warning: suggested replacement String'First + 1

I would really like to make this right, but when I try "first" it actually grabs a number, not the character.

Is there a better way to do it?

I tried looking up the 'First concept, and the below site explains I'm actually getting the number of the index, not the actual contents: http://en.wikibooks.org/wiki/Ada_Programming/Types/array

For example,

Hello_World  : constant String := "Hello World!";
World        : constant String := Hello_World (7 .. 11);
Empty_String : constant String := "";

Using 'First I'll get:

Array    'First 'Last 'Length 'Range
Hello_World   1       12       12     1 .. 12
World         7       11        5     7 .. 11
Empty_String  1        0        0     1 .. 0

Based on that information, I can't get H from Hello world (for a comparison like if Hello_World(1) = "H" then)

EDIT:
So the way I initially was doing it was
(insert some variable name instead of string in this case)

String(String'First .. String'First) = "1"

So that works from what I can tell, however, rather then writing all that, I found out that

String(String'First) = '1'

Does the same thing but using char comparison, which makes a lot more sense!

Thanks for all the answers everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏夜暖风 2024-10-15 20:12:58

对于 Ada 新手来说,字符串是最大的问题;对于那些已经是处理 Cish 语言字符串专家的人来说尤其如此。

Ada 字符串(实际上所有 Ada 数组)不像 C 那样从 0 开始,也不像 Fortran 那样从 1 开始。它们是基于编码员喜欢的方式。如果有人想从 10 ... 200 索引他们的字符串,他们可以。因此,访问 Ada 字符串中的字符的最安全方法是使用 'first 属性(或者更好的是,使用 'range'first 循环它们) ..'最后)。

在您的情况下,您似乎只想获取字符串中的第一个字符。对于名为 X 的字符串,最简单、最安全的方法是 X(X'first)

但实际上你几乎永远不会这样做。相反,您将循环遍历字符串的 'first...'last 来查找某些内容,或者仅使用 Ada.Strings.Fixed 中的一个例程。

Strings are the biggest bugaboo for newbie Ada coders; particularly so for those who are already experts at dealing with strings in Cish languages.

Ada strings (in fact all Ada arrays) are not 0 based like C, or 1-based like Fortran. They are based however the coder felt like it. If someone wants to index their string from 10 ... 200, they can. So really the safest way to acces characters in an Ada string is to use the 'first attribute (or better yet, loop through them using 'range or 'first .. 'last).

In your case it looks like you want to get at only the first character in the string. The easiest and safest way to do that for a string named X is X(X'first).

In pactice you would almost never do that though. Instead you would be looping through the string's 'first...'last looking for something, or just using one of the routines in Ada.Strings.Fixed.

心凉 2024-10-15 20:12:58

该警告建议您使用:

String(String'First + Index)

而不是仅仅

String(Index)

The warning is suggesting you use:

String(String'First + Index)

Instead of just

String(Index)
戏剧牡丹亭 2024-10-15 20:12:58

您问题中的代码有些奇怪。首先,您将变量称为“String”并且其类型为“String”。艾达会立即对此犹豫不决。

并且您为该代码片段复制的警告语句没有意义。

假设您的变量实际上称为“Value”,即:

Value : String := "34543";

Value(1) 与 Value(Value'First + 1) 不同,因为 Value'First(在此声明中)是 1。所以你最终引用了 Value(1 + 1)。您似乎遇到了这种情况,因为您提到无法在“Hello World”字符串中引用“H”。

现在警告是有效的,因为使用“First”(和“Last”和“Range”)来引用数组边界会更安全。但是,如果要从通过 'First 检索到的边界进行偏移,通常使用基于 0 或基于 1 的索引(在这种情况下需要偏移 1),则需要使用正确的索引。使用在您的上下文中更合适且更具可读性的基础。

There's something odd about the code in your question. First off, that you're calling your variable "String" and that it's of type "String". Ada will balk at that right off the bat.

And the warning statements you reproduce for that code fragment don't make sense.

Let's say your variable is actually called "Value", i.e.:

Value : String := "34543";

Value(1) is not the same as Value(Value'First + 1), because Value'First (in this declaration) is 1. So you end up referencing Value(1 + 1). You appear to be experiencing this because of mentioning that you can't reference the 'H' in a "Hello World" string.

Now the warning is valid, in that you're safer using 'First (and 'Last and 'Range) to reference array bounds. But you need to use the proper indexing if you're going to offset from the bound retrieved via 'First, typically using either 0-based or 1-based (in which case you need to offset by 1). Use whichever base is more appropriate and readable in your context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文