Ada - 有没有办法遍历无限字符串的每个字符?
Ada - Is there a way to traverse through each character of an unbounded string?
例如,我基本上有一个名为 a 的无限字符串。而a当前正在存储"Hello"。我想检查字母 O 是否在字符串中。这就是为什么我想知道是否有一种方法可以检查每个字符是否匹配以及是否存在匹配。
注意:我不想知道整个字符串是否相等,我只想知道字符串中是否有特定字符。
没那么容易。
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure str_array is
a : Unbounded_String := To_Unbounded_String("Hello");
begin
for i in 1 .. Length(a) loop
if Element(a,i) = 'A' then
null;
end if;
end loop;
end str_array;
(西蒙抢在我前面)。
教训(对我来说也是如此!)是 Unbounded_String 作为私有类型不会像 String 那样暴露内部细节。有点像C字符串和C++的区别std::string.
有很多处理固定字符串的好方法,它们大多避免了对 Unbounded_String 的需要,所以我很少使用后者,很抱歉让你误入歧途。
更简单。
if Ada.Strings.Unbounded.Count((a), "A") /= 0 then
null;
end if;
这仍然适用:String 和 Unbounded_String 都提供更高级别的工具。使用它们...
参见Ada.Unbounded_Strings.Element
(ARM A.4.5(20) and (82), and ARM A.4.4(96)):
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
procedure Ubs is
A : Unbounded_String := To_Unbounded_String ("hello");
begin
for J in 1 .. Length (A) loop
if Element (A, J) = 'o' then
Put_Line (“'o' is in A”);
exit;
end if;
end loop;
end Ubs;
我会使用索引:
if Ada.Strings.Unbounded.Index (A, "O") > 0 then
请注意,模式是一个字符串,而不是一个字符,但是搜索单字符字符串与搜索字符相同。
不过,真正的答案是您应该熟悉 ARM 的附件 A,它描述了标准库,并且在提出此类问题之前应该参考它。 ARM 可在
例如,我基本上有一个名为 a 的无限字符串。而a当前正在存储"Hello"。我想检查字母 O 是否在字符串中。这就是为什么我想知道是否有一种方法可以检查每个字符是否匹配以及是否存在匹配。
注意:我不想知道整个字符串是否相等,我只想知道字符串中是否有特定字符。
没那么容易。
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure str_array is
a : Unbounded_String := To_Unbounded_String("Hello");
begin
for i in 1 .. Length(a) loop
if Element(a,i) = 'A' then
null;
end if;
end loop;
end str_array;
(西蒙抢在我前面)。
教训(对我来说也是如此!)是 Unbounded_String 作为私有类型不会像 String 那样暴露内部细节。有点像C字符串和C++的区别std::string.
有很多处理固定字符串的好方法,它们大多避免了对 Unbounded_String 的需要,所以我很少使用后者,很抱歉让你误入歧途。
更简单。
if Ada.Strings.Unbounded.Count((a), "A") /= 0 then
null;
end if;
这仍然适用:String 和 Unbounded_String 都提供更高级别的工具。使用它们...
参见Ada.Unbounded_Strings.Element
(ARM A.4.5(20) and (82), and ARM A.4.4(96)):
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
procedure Ubs is
A : Unbounded_String := To_Unbounded_String ("hello");
begin
for J in 1 .. Length (A) loop
if Element (A, J) = 'o' then
Put_Line (“'o' is in A”);
exit;
end if;
end loop;
end Ubs;
我会使用索引:
if Ada.Strings.Unbounded.Index (A, "O") > 0 then
请注意,模式是一个字符串,而不是一个字符,但是搜索单字符字符串与搜索字符相同。
不过,真正的答案是您应该熟悉 ARM 的附件 A,它描述了标准库,并且在提出此类问题之前应该参考它。 ARM 可在