文件路径相关的字符串操作

uses
  HTTPApp, Masks;

procedure TForm1.Button1Click(Sender: TObject);
var
  ss,s: string;
begin
  //先提取一个文件名的字符串
  ss := Application.ExeName;
  ShowMessage(ss); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.exe

  //路径
  s := ExtractFilePath(ss);
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\

  //所在文件夹
  s := ExtractFileDir(ss);
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects

  //所在逻辑盘
  s := ExtractFileDrive(ss);
  ShowMessage(s); //C:

  //文件名
  s := ExtractFileName(ss);
  ShowMessage(s); //Project1.exe

  //文件扩展名
  s := ExtractFileExt(ss);
  ShowMessage(s); //.exe

  //相对路径
  s := ExtractRelativePath('C:\Documents and Settings\wy\My Documents\',ss);
  ShowMessage(s); //RAD Studio\Projects\Project1.exe

  //dos下的短文件名
  s := ExtractShortPathName(ss);
  ShowMessage(s); //C:\DOCUME~1\wy\MYDOCU~1\RADSTU~1\Projects\Project1.exe

  //转换到Unix路径格式, 需要 uses HTTPApp
  s := DosPathToUnixPath(ss);
  ShowMessage(s); //C:/Documents and Settings/wy/My Documents/RAD Studio/Projects/Project1.exe

  //转Unix路径格式为Window格式, 是 DosPathToUnixPath 的逆操作
  s := ExpandUNCFileName(DosPathToUnixPath(ss));
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.exe

  //去掉结尾的路径分隔符
  s := ExcludeTrailingPathDelimiter(ExtractFilePath(ss));
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects

  //去掉结尾的路径分隔符, 只是调用:ExcludeTrailingPathDelimiter
  s := ExcludeTrailingBackslash(ExtractFilePath(ss));
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects

  //更换扩展名, 并不修改文件, 只是替换字符串:
  s := ChangeFileExt(ss,'.bak');
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.bak

  //更换文件路径:
  s := ChangeFilePath(ss,'c:\temp');
  ShowMessage(s); //c:\temp\Project1.exe

  //判断第几个字符是否是路径分隔符:
  IsPathDelimiter(ss,3); //返回 True

  //判断后缀名是不是 exe; 需要 uses Masks
  MatchesMask(ss,'*.exe'); //返回 True
end;

 
//分解 ProcessPath (需要 uses FileCtrl) var s,p,f: string; d: Char; begin s := ParamStr(0); ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.exe ProcessPath(s,d,p,f); ShowMessage(d); //C ShowMessage(p); //\Documents and Settings\wy\My Documents\RAD Studio\Projects ShowMessage(f); //Project1.exe end;
//压缩显示 MinimizeName (需要 uses FileCtrl) var s: string; begin s := ParamStr(0); s := MinimizeName(s,Self.Canvas,100); ShowMessage(s); //C:\...\Project1.exe end;

你可能感兴趣的:(字符串)