[英]String manipulation in ada
我正在以字符串形式獲取目錄的路徑,例如"C:\\Users\\Me\\Desktop\\Hello”
,並且我試圖獲取最后一個目錄,但沒有成功。
我在字符串上嘗試了很多操作,但最終我一無所有……我將不勝感激得到一些幫助。 謝謝 !
這是我的第一個主意:
Get_Line(Line, Len);
while (Line /="") loop
FirstWord:=Index(Line(1..Len),"\")+1;
declare
NewLine :String := (Line(FirstWord .. Len));
begin
Line:=NewLine ;
end;
end loop;
我知道它不起作用(我不能將NewLine
分配給Line
因為它們的長度之間不匹配),現在我被卡住了。
我假設您要操作目錄(和文件)名稱,而不是任何舊字符串?
在這種情況下,您應該查看標准庫包Ada.Directories
( ARM A.16 )和Ada.Directories.Hierarchical_File_Names
( ARM A.16.1 ):
with Ada.Directories;
with Ada.Text_IO; use Ada.Text_IO;
procedure Tal is
Line : constant String := "C:\Users\Me\Desktop\Hello";
begin
Put_Line ("Full_Name: "
& Ada.Directories.Full_Name (Line));
Put_Line ("Simple_Name: "
& Ada.Directories.Simple_Name (Line));
Put_Line ("Containing_Directory: "
& Ada.Directories.Containing_Directory (Line));
Put_Line ("Base_Name: "
& Ada.Directories.Base_Name (Line));
end Tal;
另一方面,如果您嘗試進行純字符串操作,則可以使用類似
with Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
procedure Tal is
function Get_Last_Word (From : String;
With_Separator : String)
return String is
Separator_Position : constant Natural :=
Ada.Strings.Fixed.Index (Source => From,
Pattern => With_Separator,
Going => Ada.Strings.Backward);
begin
-- This will fail if there are no separators in From
return From (Separator_Position + 1 .. From'Last); --'
end Get_Last_Word;
Line : constant String := "C:\Users\Me\Desktop\Hello";
Last_Name : constant String := Get_Last_Word (Line, "\");
begin
Put_Line (Last_Name);
end Tal;
如您所見,將邏輯放在Get_Last_Word
可以使Last_Name
從declare
塊中提升。 但是永遠不可能用子字符串本身覆蓋固定的字符串(除非您准備好應對尾隨的空格):永遠不要嘗試更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.