【问题标题】:Delete specific part of string删除字符串的特定部分
【发布时间】:2014-07-03 08:20:19
【问题描述】:

我正在使用 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

感谢任何帮助。

附:我对帕斯卡完全陌生;我只是用它来使用 Inno Setup Compiler 编译安装程序

【问题讨论】:

  • OT:永远不要明确查询 Wow6432Node 注册表路径。而是使用直接路径和HKLM32HKLM64 作为根密钥。您可以在[Registry] 部分找到它们的描述。
  • 有什么原因吗? (注意:我的安装程序只读取注册表项,不写入任何内容。)
  • 它更容易维护,但更重要的是您将使用系统重定向器,它将您重定向到正确的注册表视图路径。谁知道呢,也许微软决定将 Wow6432Node 重命名为 Wow1234Node,这将意味着一个重定向节点,从那时起你的硬代码就会不走运。
  • 我已恢复您的编辑,因为您在收到原始答案后修改了所提出的问题。请不要那样做;它会使以前的答案无效,会使回答的人看起来很愚蠢,并且在某些情况下会损害他们的声誉,这对那些花时间试图帮助你的人来说都不是很好。如果您有新问题,请发布一个新问题。“我已经更改了我的代码(现在是这个新代码),但它仍然不起作用。它现在有这个不同的问题。”是一个新问题。这不是“进化我的代码”——而是问答。
  • 人们会期望“编辑:”位呈现全部为零,但不管我猜什么。我的错。不习惯这种风格的论坛。至于我的问题,我相信问题已经解决了。如果问题仍然存在,会通知您(带有一个新问题:D)。非常感谢你们。 :)

标签: string inno-setup pascal


【解决方案1】:

看起来你想要的是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;

【讨论】:

  • 感谢您的信息。但是我将如何实现呢? (通常我自己能弄明白这种事情,但我对语法和语言本身还是非常陌生。)
  • 这很简单。已编辑。
  • >暗示我至少知道我在做什么——不,但是,我还是不明白;将其放在我的函数末尾会在编译时出现错误。一秒钟,我尝试了一些事情。
  • >nowhere near 4chan >暗示我的事情——编辑得更加明确:)
  • @user3639133,你很接近 :-) 你只是在阻止你编译的语句末尾缺少;
猜你喜欢
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 2020-12-06
  • 2019-11-14
相关资源
最近更新 更多