![](/img/trans.png)
[英]How do you check if content of string starts with the content of another string?
[英]How do you check if string ends with another string in Ada?
每種流行語言都有這個問題的規范答案,即使該答案通常歸結為:“使用標准庫中的 string.endsWith()”。 對於 Ada,據我在 Fixed String package 的文檔中可以找到,沒有 string.endswith function。
那么,給定兩個固定字符串 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 建立一個標准答案。
稍微簡化一下西蒙的回答:
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;
好吧,這是一個可能的解決方案:
主文件
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
$ ./main
Yay!
更新 (2)
另一個更新(感謝@Brian Drummond 的評論;不過評論消失了),再次使用Tail
。 這現在幾乎與@Zerte 的答案相同,除了對Ada.Strings.Fixed
的依賴:
主文件
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
$ ./main
All OK.
這是一個沒有任何顯式循環的示例。
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;
作為對吉姆答案的略微簡化,這也適用:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.