简体   繁体   English

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

[英]How do you check if string ends with another string in Ada?

There are canonical answers to this question for every popular language, even though that answer usually boils down to: "Use string.endsWith() from the standard library".每种流行语言都有这个问题的规范答案,即使该答案通常归结为:“使用标准库中的 string.endsWith()”。 For Ada, as far as I can find in the docs for the Fixed String package , there is no string.endswith function.对于 Ada,据我在 Fixed String package 的文档中可以找到,没有 string.endswith function。

So, given two fixed strings A and B, how do you check if A ends with B?那么,给定两个固定字符串 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

My intent is to establish a standard answer for Ada.我的目的是为 Ada 建立一个标准答案。

A slight simplification of Simon's answer:稍微简化一下西蒙的回答:

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;

Well, here's a possible solution:好吧,这是一个可能的解决方案:

main.adb主文件

with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

procedure Main is

   A : constant String := "John Johnson";
   B : constant String := "son";

begin

   if Tail (A, B'Length) = B then
      Put_Line ("Yay!");
   end if;

end Main;

output output

$ ./main
Yay!

UPDATE (2)更新 (2)

Another update (thanks @Brian Drummond for the comment; comment disappeared though), again using Tail .另一个更新(感谢@Brian Drummond 的评论;不过评论消失了),再次使用Tail This is now almost identical to @Zerte's answer, except for the dependency on Ada.Strings.Fixed :这现在几乎与@Zerte 的答案相同,除了对Ada.Strings.Fixed的依赖:

main.adb主文件

with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Assertions;    use Ada.Assertions;

procedure Main is

   function Ends_With (Source, Pattern : String) return Boolean is
   begin
      return Source'Length >= Pattern'Length and then
        Tail (Source, Pattern'Length) = Pattern;
   end Ends_With;   

begin

   Assert (Ends_With ("John Johnson", "son")  = True);
   Assert (Ends_With ("hi", "longer than hi") = False);

   Assert (Ends_With (""  , ""  ) = True);
   Assert (Ends_With (" " , ""  ) = True);
   Assert (Ends_With (""  , " " ) = False);
   Assert (Ends_With (" " , " " ) = True);

   Assert (Ends_With ("n ", "n ") = True);
   Assert (Ends_With (" n", "n" ) = True);
   Assert (Ends_With ("n" , " n") = False);
   Assert (Ends_With (" n", " n") = True);

   Put_Line ("All OK.");

end Main;

output output

$ ./main
All OK.

Here is an example without any explicit loops.这是一个没有任何显式循环的示例。

with Ada.Assertions; use Ada.Assertions;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   function Ends_With(Source : String; Pattern : String) return Boolean is
      result : Boolean := False;
   begin
      if Pattern'Length <= Source'Length then
         if Pattern'Length > 0 then
            result := Source((Source'Last - Pattern'Length + 1)..Source'Last) = Pattern;
         else 
            result := True;
         end if;
      end if;
      return result;
   end Ends_With;

begin

   Assert (Ends_With ("John Johnson", "son") = True);


   Assert (Ends_With (""  , ""  ) = True);
   Assert (Ends_With (" " , ""  ) = True);
   Assert (Ends_With (""  , " " ) = False);
   Assert (Ends_With (" " , " " ) = True);   

   Assert (Ends_With (""  , "n" ) = False);
   Assert (Ends_With ("n"  , "" ) = True);

   Assert (Ends_With ("n ", "n ") = True);
   Assert (Ends_With (" n", "n" ) = True);
   Assert (Ends_With ("n" , " n") = False);
   Assert (Ends_With (" n", " n") = True);

   Put_Line("All OK");
end Main;

As a slight simplification of Jim's answer , this works too:作为对吉姆答案的略微简化,这也适用:

   function Ends_With (Source, Pattern : String) return Boolean is
   begin
      if Pattern'Length > Source'Length then
         return False;
      else
         return Source (Source'Last - Pattern'Length + 1 .. Source'Last)
           = Pattern;
      end if;
   end Ends_With;

but, even better (thanks, Zerte),但是,甚至更好(谢谢,Zerte),

function Ends_With (Source, Pattern : String) return Boolean is
  (Pattern'Length <= Source'Length and then
     Source (Source'Last - Pattern'Length + 1 .. Source'Last) = Pattern);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查字符串的内容是否以另一个字符串的内容开头? - How do you check if content of string starts with the content of another string? 如何检查字符串是否以特定子字符串结尾? - How can you check if a string ends with a specific substring? 在Haskell中,如何检查字符串是否是另一个字符串的子字符串? - In Haskell, how do you check whether a string is a substring of another? 如何检查字符串是否以 .txt 结尾 - How to check if string ends with .txt 检查一个字符串是否以另一个字符串结尾或另一个字符串的一部分 - Check if a string ends with another string or a part of another string 如何检查一个字符串是否以另一个字符串结尾,或者相反? - How to check if one string ends with another, or the other way around? 你如何编写一个程序,你可以在一个字符串中输入最多 5 个字符,但也可以少于 5 个(在 Ada 中)? - How do you write a program where you can enter a maximum of 5 characters in a string, but also less than 5(in Ada)? 如何使用PHP检查文本字符串是否以“ .PDF”结尾? - How Do I Check If a String of Text Ends With '.PDF' Using PHP? 如何检查字符串是否以精确字符串结尾? - how to check whether the string ends with exact string? 如何确定一个字符串是否以 R 中的另一个字符串“结尾”? - How to determine if a string “ends with” another string in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM