你如何检查字符串是否以 Ada 中的另一个字符串结尾?

Tam*_*inn 3 string ada

对于每种流行语言,这个问题都有规范的答案,即使该答案通常归结为:“使用标准库中的 string.endsWith()”。对于 Ada,据我在固定字符串包的文档中可以找到,没有 string.endswith 函数。

那么,给定两个固定字符串 A 和 B,如何检查 A 是否以 B 结尾?

declare
   A : constant String := "John Johnson";
   B : constant String := "son";
begin
   if A.Ends_With(B) then -- this doesn't compile
      Put_Line ("Yay!");
   end if;
end
Run Code Online (Sandbox Code Playgroud)

我的目的是为 Ada 建立一个标准答案。

Zer*_*rte 6

西蒙的回答略有简化:

function Ends_With (Source, Pattern : String) return Boolean is
begin
   return Pattern'Length <= Source'Length 
     and then Source (Source'Last - Pattern'Length + 1 .. Source'Last) = Pattern;
end Ends_With;
Run Code Online (Sandbox Code Playgroud)