要求如下,若干文本,有若干SQL语句,后面附有注释,
注释符包括:1.行注释符://、--
            2.段注释符:/*   */
                        {     }
例如:select a from xx where ...// a:name
{
  段注释部分

create table.... //
drop........//程序目的:去掉所规定的各种注释符部分请各位高手,指点~~~~~~菜鸟,不胜感激!

解决方案 »

  1.   

    Rail100what do you mean?
      

  2.   

    procedure TForm1.DelRem(FileName:string);
    var
      s:string;
      i:integer;
      BraLPos:integer;
      BraRPos:integer;
      RemLen:integer;
      FileStore:TStringList;
    begin
      FileStore:=TStringList.Create;
      //读文件
      FileStore.LoadFromFile(FileName);
      //先删除段注释
      BraLPos:=Pos('{',FileStore.Text);
      while BraLPos<>0 do
      begin
        BraRPos:=Pos('}',FileStore.Text);
        if BraRPos=0 then
          BraRPos:=Length(FileStore.Text);
        RemLen:=BraRPos-BraLPos+1;
        s:=FileStore.Text;
        Delete(s,BraLPos,RemLen);
        FileStore.Text:=s;
        BraLPos:=Pos('{',FileStore.Text);
      end;
      //再删除行注释
      for i:=0 to FileStore.Count-1 do
      begin
        s:=FileStore.Strings[i];
        BraRPos:=Pos('//',s);
        if BraRPos<>0 then
          Delete(s,BraRPos,Length(s));
        FileStore.Strings[i]:=s;
      end;
      //保存文件
      FileStore.SaveToFile(FileName);
    end;
      

  3.   

    啊,对不起!我没考虑字符串里的情况!!!
    上面的程序只考虑了段注释里的行注释,但是没考虑字符串里情况!
    真抱歉,一开始就应该看看YFLK(远方来客) 说的!
      

  4.   

    这就是伴水写的
    怎样除去源代码的注释 问题:怎样除去源代码的注释>>不简单哦No.1 编译指令不能除(如:{$I+})No.2 字符表达式中不能除(如:'{ 我不是注释,我是字符串 }')No.3 其他情况 如: begin  //{要小心哦  Inc(I);//}end;  type  TModalStr = record    rBegin: string;    rEnd: string;    rAppend: string;    rSingle: Byte;  end; const  cPascalCount = 5;  cPascalList: array[0 .. Pred(cPascalCount)] of TModalStr =((rBegin: ''''; rEnd: '''';   rAppend: '';     rSingle: 1),(rBegin: '{$'; rEnd: '}';    rAppend: '';     rSingle: 2),(rBegin: '{';  rEnd: '}';    rAppend: '';     rSingle: 3),(rBegin: '(*'; rEnd: '*)';   rAppend: '';     rSingle: 4),(rBegin: '//'; rEnd: #13#10; rAppend: #13#10; rSingle: 5)); const  cSQLCount = 4;  cSQLList: array[0 .. Pred(cSQLCount)] of TModalStr =((rBegin: ''''; rEnd: '''';   rAppend: '';     rSingle: 1),(rBegin: '"';  rEnd: '"';    rAppend: '';     rSingle: 2),(rBegin: '/*'; rEnd: '*/';   rAppend: '';     rSingle: 3),(rBegin: '--'; rEnd: #13#10; rAppend: #13#10; rSingle: 4)); function GetModalStr(mStr: string; mModalStrList: array of TModalStr;  mSingles: TIntegerSet): string;var  vSingle: Integer;  I, J: Integer;  T, K: Integer;  vEnd: string;  vAppend: string;begin  Result := '';  vSingle := 0;  T := 0;  K := 0;  for I := 1 to Length(mStr) do begin    if T > 1 then begin      Dec(T);      Continue;    end;    if vSingle = 0 then begin      vEnd := '';      for J := Low(mModalStrList) to High(mModalStrList) do begin        K := Length(mModalStrList[J].rBegin);        if Copy(mStr, I, K) = mModalStrList[J].rBegin then begin          vEnd := mModalStrList[J].rEnd;          vAppend := mModalStrList[J].rAppend;          vSingle := mModalStrList[J].rSingle;          if vSingle in mSingles then            Result := Result + mModalStrList[J].rBegin;          T := K;          K := Length(vEnd);          Break;        end;      end;      if (vEnd = '') and (vSingle in mSingles) then        Result := Result + mStr[I];    end else if Copy(mStr, I, K) = vEnd then begin      if vSingle in mSingles then        Result := Result + vEnd      else Result := Result + vAppend;      vSingle := 0;      T := K;    end else if vSingle in mSingles then      Result := Result + mStr[I];  end;end; { GetModalStr } procedure TForm1.Button1Click(Sender: TObject);begin  Memo2.Text := GetModalStr(Memo1.Text, cPascalList, [0..cPascalCount] - [3, 4, 5]);end; procedure TForm1.Button2Click(Sender: TObject);begin  Memo2.Text := GetModalStr(Memo1.Text, cSQLList, [0..cSQLCount] - [3, 4]);end;