下面是我临时编的小程序,只能处理标准的文本文件,并且程序中没进行错误
检查,希望对你有些帮助:
//将一字符串中的空格去掉
function TrimSpaceChar(S:AnsiString):String;
var
NewStr
Char; //新的字符串
CurrentCharIn,CurrentCharOut
Char;//字符的指针
isGBCode:Boolean;
begin
GetMem(NewStr,StrLen(PChar(S)));
CurrentCharIn:=PChar(S);
CurrentCharOut:=PChar(NewStr);
isGBCode:=False;
while PBYTE(CurrentCharIn)^<>0 do
begin
//判断是否是中文字符
if (PBYTE(CurrentCharIn)^>$9F) and (PBYTE(CurrentCharIn)^<$FF) then
begin
isGBCode:=True;
PWORD(CurrentCharOut)^:=PWORD(CurrentCharIn)^;
Inc(CurrentCharOut,2);
Inc(CurrentCharIn,2);
end
else
begin
//如果上一个字是中文,空隔去掉,否则不用去掉
if (PBYTE(CurrentCharIn)^=$20) and isGBCode then
Inc(CurrentCharIn,1)
else
begin
//处理英文字符
isGBCode:=False;
PBYTE(CurrentCharOut)^:=PBYTE(CurrentCharIn)^;
Inc(CurrentCharOut,1);
Inc(CurrentCharIn,1);
end;
end;
end;
PBYTE(CurrentCharOut)^:=0;
result:=StrPas(NewStr);
FreeMem(NewStr);
end;
//转换一个文本文件
procedure ConvertFile(InFileName,OutFileName:TFileName);
var
FInHandle,FOutHandle:TextFile;
TmpStr:AnsiString;
begin
AssignFile(FInHandle,InFileName);
AssignFile(FOutHandle,OutFileName);
Reset(FInHandle);
ReWrite(FOutHandle);
while not EOF(FInHandle) do
begin
ReadLn(FInHandle,TmpStr);
WriteLn(FOutHandle,TrimSpaceChar(TmpStr));
end;
CloseFile(FInHandle);
CloseFile(FOutHandle);
end;