我正在获取字符串中的目录路径,例如"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块中提升.但是永远不可能用自己的子串覆盖一个固定的字符串(除非你准备好处理尾随的空白,就是这样):永远不要尝试.
| 归档时间: | 
 | 
| 查看次数: | 511 次 | 
| 最近记录: |