如何在 Ada 中检查字符串是否以另一个字符串开头或结尾?

问题描述 投票:0回答:4

对于每种流行语言,这个问题都有规范的答案,尽管该答案通常归结为:“使用标准库中的 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

我的目的是为Ada建立一个标准答案。知道如何检查一个字符串是否以另一个字符串开头也很好。

string ada
4个回答
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;

类似的 Starts_With 是:

function Starts_With (Source, Pattern : String) return Boolean is begin return Pattern'Length <= Source'Length and then Source (Source'First .. Source'First + Pattern'Length - 1) = Pattern; end Starts_With;



4
投票

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;

输出

$ ./main Yay!


更新(2)

另一个更新(感谢@Brian Drummond 的评论;不过评论消失了),再次使用

Tail

。现在,这与 @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;

输出

$ ./main All OK.



2
投票

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;



2
投票
Jim 的答案

的稍微简化,这也有效: 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;

但是,更好(谢谢,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);

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.