关于文本覆盖,我这个代码会什么执行不了?
大虾们帮看看..
将一个TXT文件中的含有#21251 之类的Unicode 转为 汉字,
procedure TForm1.Button5Click(Sender: TObject);
var
J,i,P:Integer;
s:String;
List:TStringList;
Str, ReplStr: string;
begin
List := TStringList.Create;
List.LoadFromFile(trim(edit3.Text));
for J := 0 to List.Count - 1 do
begin
s := List[J];
i := pos(trim(str),s);
//当#26597
if (List[J]='#') and (IsNumeric(List[J+1])) and (IsNumeric(List[J+2])) and (IsNumeric(List[J+3])) and (IsNumeric(List[J+4])) and (IsNumeric(List[J+5])) then
Str:=List[J]+List[J+1]+List[J+2]+List[J+3]+List[J+4]+List[J+5];
//当#32 (#32表示空格)
if (List[J]='#') and (IsNumeric(List[J+1])) and (IsNumeric(List[J+2])) and (List[J+2]='#') then
Str:=List[J]+List[J+1]+List[J+2];
//开始转换
ReplStr:=UnicodeToAnsi(IntToHex(StrToInt(Str),4));
P := Length( trim(str) );
Delete( s, i, P );
insert( ReplStr, s, i );
List[ J ]:=s;
break;
end;
List.SaveToFile(trim(Edit4.Text));
List.free;
end;
[解决办法]
写得够乱的
str开始就没有赋初值
循环中无条件使用break
List[J + 1]、List[J + 2]...
难道楼主当成了第J行的第几个字符-_-!!!
参考如下代码:
- Delphi(Pascal) code
procedure TForm1.Button1Click(Sender: TObject);var I: Integer; S, T, W: string; B: Boolean; // 是否处于#中 List: TStringList;begin List := TStringList.Create;// List.LoadFromFile(Trim(Edit3.Text)); List.Text := Memo1.Text; S := List.Text; T := ''; W := ''; B := False; for I := 1 to Length(S) do begin if B then begin case S[I] of '0'..'9': W := W + S[I]; else begin T := T + WideChar(StrToIntDef(W, Ord('#'))); B := S[I] = '#'; if not B then T := T + S[I]; W := ''; end; end; end else begin case S[I] of '#': B := True; else T := T + S[I]; end; end; end; if B then T := T + WideChar(StrToIntDef(W, Ord('#'))); Memo2.Text := T; //List.SaveToFile(Trim(Edit4.Text)); List.Free;end;