function MyCopy(source:string;aSize:Integer):string; var r1,r2:string; w1,w2:widestring; begin r1:=Copy(source,1,aSize+1); r2:=Copy(source,1,aSize); w1:=r1; w2:=r2; if Length(w1)=Length(w2) then begin result:=copy(source,1,aSize-1); end else Result:=r2; end;procedure TForm1.Button1Click(Sender: TObject); var s1,s2:string; begin s1:='Edit01000中国'; s2:=MyCopy(s1,10); ShowMessage(s2);end;
s := Copy(Edit1.Text, 1, 59) + Copy(WideString(Edit1.Text), 10, 60);
这个我测试了,没有问题的,能很好处理汉字尾巴 function TForm1.get60(str: String; len: Integer): string; var i, n, ntotal, nstep: integer; wstr:WideString; begin wstr:=WideString(str); ntotal := 0; result := ''; for i := 1 to length(wstr) do begin if WORD(wstr[i]) > 256 then nstep := 2 else nstep := 1; if ntotal + nstep <= len then begin result := result + wstr[i]; ntotal := ntotal + nstep; end else exit; end; end;procedure TForm1.BitBtn1Click(Sender: TObject); var st, s: string; begin st := 'ddd我们cvb的祖国'; s := get60(st, 13); ShowMessage(s); end;
s := Copy(Edit1.Text, 1, 59) + Copy(WideString(Edit1.Text), 60, 1);
字符“Edit10000中国”,我用MidBStr(Edit1.Text, 1, 10)截
端后,借端10个字符,变成了“Edit10000[”,最后一个是乱
码,转到数据库中是个空格,其实不是个空格,是个乱码,只
是无法显示而已。
var
r1,r2:string;
w1,w2:widestring;
begin
r1:=Copy(source,1,aSize+1);
r2:=Copy(source,1,aSize); w1:=r1;
w2:=r2;
if Length(w1)=Length(w2) then
begin
result:=copy(source,1,aSize-1);
end
else
Result:=r2;
end;procedure TForm1.Button1Click(Sender: TObject);
var
s1,s2:string;
begin
s1:='Edit01000中国';
s2:=MyCopy(s1,10);
ShowMessage(s2);end;
function TForm1.get60(str: String; len: Integer): string;
var
i, n, ntotal, nstep: integer;
wstr:WideString;
begin
wstr:=WideString(str);
ntotal := 0;
result := '';
for i := 1 to length(wstr) do
begin
if WORD(wstr[i]) > 256 then nstep := 2 else nstep := 1;
if ntotal + nstep <= len then
begin
result := result + wstr[i];
ntotal := ntotal + nstep;
end
else
exit;
end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
st, s: string;
begin
st := 'ddd我们cvb的祖国';
s := get60(st, 13);
ShowMessage(s);
end;
要不就用widestring