删除字符串的特定部分

时间:2014-05-15 03:40:32

标签: string inno-setup pascal

我使用Pascal脚本来返回基于注册表项的字符串,但我需要删除该字符串的特定部分。

这是当前的代码:

function GetDirName(Value: string): string;
var          
  InstallPath: string;
begin
  // initialize default path, which will be returned when the following registry
  // key queries fail due to missing keys or for some different reason
  Result := '{pf}\LucasArts\Star Wars Battlefront II\RegistryFailed';
  // query the first registry value; if this succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath
  // otherwise the first registry key query failed, so...
  else
  // query the second registry value; if it succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\Wow6432Node\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath
end;

我需要从返回的字符串中删除的部分是 \ BattlefrontII.exe ,它始终位于字符串的最后。

当前返回的字符串的示例:C:\Program Files (x86)\LucasArts\Star Wars Battlefront II\GameData\BattlefrontII.exe

我需要返回的字符串看起来或多或少的示例:C:\Program Files (x86)\LucasArts\Star Wars Battlefront II\GameData

感谢任何帮助。

P.S。我对帕斯卡来说是全新的;我只使用它来使用Inno Setup Compiler编译安装程序

1 个答案:

答案 0 :(得分:3)

看起来你想要的是ExtractFilePath

function ExtractFilePath(const FileName: string): String;
  

描述:提取给定文件的驱动器和目录部分   名称。结果字符串是FileName的最左侧字符,向上   包括分隔路径的冒号或反斜杠   来自名称和扩展名的信息。结果字符串为空   如果FileName不包含驱动器和目录部件。

有关更多实用内容,请参阅list of functions

在你的情况下,你可以把它:

Result := ExtractFilePath(Result);

在你的功能结束时。

编辑:要明确:

function GetDirName(const Value: string): string;
var          
  InstallPath: string;
begin
  // initialize default path, which will be returned when the following registry
  // key queries fail due to missing keys or for some different reason
  Result := ExpandConstant('{pf}\LucasArts\Star Wars Battlefront II\RegistryFailed');
  // query the first registry value; if this succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath  
  // otherwise the first registry key query failed, so...
  else
  // query the second registry value; if it succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\Wow6432Node\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath;

  // return only the path
  Result := ExtractFilePath(Result);
end;