type
TFilterLetter=set of char;//过滤掉文件名中的非法字符
function FilterFileName(s:string):string;
var
FilterLetter:TFilterLetter;
i:integer;
begin
FilterLetter:=[':','/','\','|','*','?','<','>','"'];
for i:=0 to length(s)-1 do
  begin
    if s[i] in FilterLetter then delete(s,i,1);
  end;
FilterFileName:=s;
end;我的代码对吗?有没有更好的方法?谢谢

解决方案 »

  1.   

    挺好的。支持你!返回值如果用Result,不用函数名,就更好了。
    不是说函数名不能用,而是说用Result更好
      

  2.   

    这样写也可以,就不用那个set of char的类型定义了function FilterFileName(s:string):string;
    var
    i:integer;
    begin
    for i:=0 to length(s)-1 do
      begin
        if s[i] in [':','/','\','|','*','?','<','>','"'] then delete(s,i,1);
      end;
    FilterFileName:=s;
    end;
      

  3.   


    汗自己。改一下。注意for循环变量是从1到Length(s)function FilterFileName(s:string):string;
    var
    i:integer;
    begin
    for i:=1 to length(s) do
      begin
        if s[i] in [':','/','\','|','*','?','<','>','"'] then delete(s,i,1);
      end;
    Result:=s;
    end;
      

  4.   

    我那句delete(s,i,1);是不是错了,应该这样吗delete(s,i,i+1);
      

  5.   

    function FilterFileName(s: string): string;
    var
    FilterLetter:TFilterLetter;
    i:integer;
    begin
    FilterLetter:=[':','/','\','|','*','?','<','>','"'];
    for i:=length(s)  downto 1 do
      begin
        if s[i] in FilterLetter then delete(s,i,1);
      end;
    FilterFileName:=s;
    end;
      

  6.   

    再汗。for i := length(s) downto 1 就对了
      

  7.   


    有区别。测试一下就知道了:procedure TForm1.Button2Click(Sender: TObject);
    begin
      Showmessage(FilterFileName('??????a\b:c|d<e>f"g*a<<<<<'));
    end;
      

  8.   


    delete(s,i+1,1);string的第一个字符下标是从1开始的
      

  9.   

    有一个奇怪现象
    showmessage(filterfilename('優質出品:春天的祝福'))
    执行后变成乱码,是否会生成新的非法字符。
      

  10.   

    function FilterFileName(s: String): String;
    var
      FilterLetter: set of Char;
      i: Integer;
    begin
      FilterLetter := [':','/','\','|','*','?','<','>','"'];
      i := 1;
      while i <= Length(s) do
      begin
        if Ord(s[i]) > $7F then
          Inc(i, 2)
        else
        begin
          if s[i] in FilterLetter then
            Delete(s,i,1)
          else
            Inc(i);
        end;
      end;
      Result := s;
    end;
      

  11.   

    type
      WS = WideString;function FilterFileName(s: WideString): WideString;
    var
    i:integer;
    begin
      for i:=length(s)  downto 1 do
        begin
          if s[i] in [WS(':')[1],WS('/')[1],WS('\')[1],WS('|')[1],WS('*')[1],WS('?')[1],WS('<')[1],WS('>')[1],WS('"')[1]]
           then delete(s,i,1);
        end;
      Result:=s;
    end;
      

  12.   


    type
      TFilterLetter=set of char;//过滤掉文件名中的非法字符
    function FilterFileName(const S: string): string;
    var
      R: PChar;
      FilterLetter: TFilterLetter;
      I, L: integer;
    begin
      L := Length(S);
      SetLength(Result, L);
      Result := StringOfChar(#0, L);
      R := PChar(Result);
      FilterLetter := [':','/','\','|','*','?','<','>','"'];
      for I := 1 to L do
        if not (s[i] in FilterLetter) then 
        begin
           R^ := S[I];
           Inc(R);
        end;
      SetLength(Result, R - PChar(Result));
    end;
      

  13.   

    function FilterFileName(const ASource: string): string;
    const
      FilterLetter = [':', '/', '\', '|', '*', '?', '<', '>', '"'];
    var
      S, R: PChar;
      I, L: integer;
    begin
      L := Length(ASource);
      SetLength(Result, L);
      Result := StringOfChar(#0, L);
      R := PChar(Result);
      S := PChar(ASource);
      for I := 1 to L do
      begin
        if S^ > #127 then
        begin
          R^ := S^;
          Inc(R);
          Inc(S);
          R^ := S^;
        end else
        if not (S^ in FilterLetter) then
          R^ := S^;
        Inc(R);
        Inc(S);
      end;
      SetLength(Result, R - PChar(Result));
    end;
      

  14.   

    function FilterFileName(sName:string):String;
    var
      I :Integer;
    begin
      Result := '';
      for I := Length(sName) downto 1 do
      begin
        if sName[I] in [':', '/', '\', '|', '*', '?', '<', '>', '"'] then
          Delete(sName, I, 1);
      end;
      Result := sName;
    end;