function GetStrLength(const S: string): integer; begin Result:=Length(WideString(S)); end;
procedure TForm1.Button1Click(Sender: TObject); var s1, s2, s3: WideString; i: integer; begin s1:='我是一个好人啊lilor'; s2:='我是一个坏人吗lilor'; for i:=1 to length(s1) do begin if (pos(s1[i],s3)=0)and(pos(s1[i],s2)=0) then s3:=s3+s1[i]; if (pos(s1[2],s3)=0)and(pos(s2[i],s1)=0) then s3:=s3+s2[i]; end; ShowMessage(Format('不同的字符有%d, 它们是(%s)', [length(s3), s3])); end;运行结果 --------------------------- test --------------------------- 不同的字符有4, 它们是(好坏啊吗) --------------------------- OK ---------------------------
这句if (pos(s1[2],s3)=0)and(pos(s2[i],s1)=0) then s3:=s3+s2[i];写错了 应该是if (pos(s2[i],s3)=0)and(pos(s2[i],s1)=0) then s3:=s3+s2[i];
var str1,str2: String; StrLenth,i,Count: Integer; begin str1 :='fdfasdfdasf'; str2 :='345wgsdgreg'; StrLenth := Length(str1); i := StrLenth; j := i; for i := 1 to StrLenth do begin for j := 1 to StrLenth do begin If Copy(str1,i,1) = Copy(str2,j,1) then Break else If j = StrLenth then Count := Count +1; end; end; end;
var WStr:WideString; AStr:String; begin AStr = "123456中国文字!" WStr = WideString(AStr); Showmessage(format("按英文计算长度 %d",[length(AStr)])); Showmessage(format("按混合计算长度 %d",[length(WStr)])); Showmessage(format("取中文数 %d",[length(WStr)-Length(AStr)])); end; 还有问题吗?
比较的时候用WideString 类型即可。
var s1,s2,s3:string; i:integer; begin s1 := edit1.Text; s2 := edit2.Text; s3 := ''; if length(s1) <> length(s2) then showmessage('两个串不等长!') else begin for i := 1 to length(s1) do begin if pos(s2[i],s1)=0 then s3 := s3 + s2[i]; if pos(s1[i],s2)=0 then s3 := s3 + s1[i]; end; edit3.Text := '共有'+inttostr(length(s3))+'个字符不同,他们是:'+s3; end; end;
begin
Result:=Length(WideString(S));
end;
var
s1, s2, s3: WideString;
i: integer;
begin
s1:='我是一个好人啊lilor';
s2:='我是一个坏人吗lilor';
for i:=1 to length(s1) do
begin
if (pos(s1[i],s3)=0)and(pos(s1[i],s2)=0) then s3:=s3+s1[i];
if (pos(s1[2],s3)=0)and(pos(s2[i],s1)=0) then s3:=s3+s2[i];
end;
ShowMessage(Format('不同的字符有%d, 它们是(%s)', [length(s3), s3]));
end;运行结果
---------------------------
test
---------------------------
不同的字符有4, 它们是(好坏啊吗)
---------------------------
OK
---------------------------
应该是if (pos(s2[i],s3)=0)and(pos(s2[i],s1)=0) then s3:=s3+s2[i];
str1,str2: String;
StrLenth,i,Count: Integer;
begin
str1 :='fdfasdfdasf';
str2 :='345wgsdgreg';
StrLenth := Length(str1);
i := StrLenth;
j := i;
for i := 1 to StrLenth do
begin
for j := 1 to StrLenth do
begin
If Copy(str1,i,1) = Copy(str2,j,1) then Break
else If j = StrLenth then Count := Count +1;
end;
end;
end;
AStr:String;
begin
AStr = "123456中国文字!"
WStr = WideString(AStr);
Showmessage(format("按英文计算长度 %d",[length(AStr)]));
Showmessage(format("按混合计算长度 %d",[length(WStr)]));
Showmessage(format("取中文数 %d",[length(WStr)-Length(AStr)]));
end;
还有问题吗?
s1,s2,s3:string;
i:integer;
begin
s1 := edit1.Text; s2 := edit2.Text; s3 := '';
if length(s1) <> length(s2) then
showmessage('两个串不等长!')
else
begin
for i := 1 to length(s1) do
begin
if pos(s2[i],s1)=0 then s3 := s3 + s2[i];
if pos(s1[i],s2)=0 then s3 := s3 + s1[i];
end;
edit3.Text := '共有'+inttostr(length(s3))+'个字符不同,他们是:'+s3;
end;
end;