怎么才能到序取字符串呢?例如:一串字符abc:= D:/文件/XX../图片-RGS.gif取得abc中的“图片-RGS”这几个字符。

解决方案 »

  1.   

    const
      PathDelim = '/';function ExtractFileName(const FileName: string): string;
    var
      I: Integer;
    begin
      I := LastDelimiter(PathDelim + DriveDelim, FileName);
      Result := Copy(FileName, I + 1, MaxInt);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(ExtractFileName('D:/文件/XX../图片-RGS.gif'));
    end;
      

  2.   

    谢谢哇,我明白了,不过输出的是‘图片-RGS.gif’,需要的是‘图片-RGS‘。就是说还要定义一个Delimiter ,不过这次要定义"."为分割符应该怎么定义呢?
      

  3.   

    建议楼主要学会看Delphi Help
      帮助主题中输入:LastDelimiter
    VCL Reference
    LastDelimiter functionSee also
    Returns the byte index in S of the last character that matches any character in the Delimiters AnsiString.UnitSysUtilsCategorystring handling routinesfunction LastDelimiter(const Delimiters, S: string): Integer;DescriptionCall LastDelimiter to locate the last delimiter in S. For example, the lineMyIndex := LastDelimiter('\.:','c:\filename.ext');sets MyIndex to 12.When working with multi-byte character sets (MBCS), S may contain double byte characters, but the delimiters listed in the Delimeters parameter must all be single byte non-null characters.
      

  4.   

    ExtractFileName
    ExtractFilePath
    ExtractFileExt
    这些函数都可以取出文件名中的相关部分,看看帮助里面怎么说的.
      

  5.   

    //楼主的路径为什么要用“/”
    //难道是Kylix?
    const
      PathDelim = '/';function ExtractFileName(const FileName: string): string;
    var
      I: Integer;
    begin
      I := LastDelimiter(PathDelim + DriveDelim, FileName);
      Result := Copy(FileName, I + 1, MaxInt);
    end;function ChangeFileExt(const FileName, Extension: string): string;
    var
      I: Integer;
    begin
      I := LastDelimiter('.' + PathDelim + DriveDelim,Filename);
      if (I = 0) or (FileName[I] <> '.') then I := MaxInt;
      Result := Copy(FileName, 1, I - 1) + Extension;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(ChangeFileExt(ExtractFileName('D:/文件/XX../图片-RGS.gif'), ''));
    end;
      

  6.   

    先谢谢大家的热心解答.现在是这样的情况:有一批图片,名称都是:图片-XXX.gif这个格式的.  '图片'是一些物品的名称,'XXX'是图片里面的物品的型号.需要把这些图片上传到网站上手工弄起来很麻烦的,因为要根据这些图片的名称相应的填写网站上的相关选项.所以就想做一个简易的浏览器,自动获取图片名称,根据图片的名称自动填写表格的相关选项.
      

  7.   

    学习DELPHI时间不长,原本以为需要倒序取呢,看来名字起错了..
      

  8.   

    RigthStr()
    或者将其反转过来,从前面取,再搞回去,哈哈