试试这个代码呢function func (Const S1, S2: string; boFlag :Boolean = True{是否忽略大小写} ): integer; var nLen, nLen2, n: integer; ch1, ch2 :Char; p1, p2 :PChar; begin result := -1; nLen := length (S1); nLen2 := length (S2); p1 := PChar (S1); p2 := PChar (S2); n := -1; for n := 0 to nLen - 1 do begin if n = nLen2 then begin result := nLen2; break; end; ch1 := p1[n]; ch2 := p2[n]; //忽略大小写 先转换为大写 if boFlag then begin if ch1 in ['a'..'z'] then ch1 := Char (byte (ch1) - $20); if ch2 in ['a'..'z'] then ch2 := Char (byte (ch2) - $20); end; if ch1 <> ch2 then begin result := n + 1; break; end; end; end;
小弟后来完成了,呵呵,谢谢大家了,代码贴出来参考参考 function StrCompare(s1:string;s2:string): string; var Num,i:Integer; num1,num2:string; begin Result:=''; for i:=1 to Length(s1) do begin if (CompareText(S1[i],S2[i])<>0) then begin num1:=copy(s1,i,Length(s1)-i+1); num2:=copy(s2,i,Length(s2)-i+1); Num:=strtoint(num2)-strtoint(num1) ; result:=inttostr(Num); exit ; end; end; end;
function gn(str1,str2: string): integer; var i,max: integer; begin if length(str1) > length(str2) then max:= length(str2) else max:= length(str1); for i:= 1 to max do begin if copy(str1,1,i) <> copy(str2,1,i) then begin result:= i; exit; end; end; end;
其实8楼的想法和我的是基本相同的:function func (Const S1, S2: string; boFlag :Boolean = True{是否忽略大小写} ): integer; var nLen, nLen2, n: integer; ch1, ch2 :Char; p1, p2 :PChar; begin result := -1; nLen := length (S1); nLen2 := length (S2); p1 := PChar (S1); //使用中间变量的目的是为了代码易懂,而且并不会影响速度,因为只有一次mov 操作,不涉及到循环 p2 := PChar (S2); for n := 0 to nLen - 1 do begin //如果到达比较短的那个字符串的结束位置,则终止循环 if n = nLen2 then begin result := nLen2; break; end;
//为了进行大小写转换,用了个中间变量,保存当前操作的字节,如果不考虑大小写参数,可以通过简单比较S1[n+1]与S2[n+1]进行操作 ch1 := p1[n]; ch2 := p2[n]; //忽略大小写 先转换为大写 if boFlag then begin if ch1 in ['a'..'z'] then ch1 := Char (byte (ch1) - $20); if ch2 in ['a'..'z'] then ch2 := Char (byte (ch2) - $20); end;
if ch1 <> ch2 then begin result := n + 1; break; end; end; end;
每个字符就是str1[I]比较到了就break就行
var nLen, nLen2, n: integer;
ch1, ch2 :Char;
p1, p2 :PChar;
begin
result := -1;
nLen := length (S1);
nLen2 := length (S2); p1 := PChar (S1);
p2 := PChar (S2); n := -1;
for n := 0 to nLen - 1 do begin
if n = nLen2 then begin
result := nLen2;
break;
end; ch1 := p1[n];
ch2 := p2[n]; //忽略大小写 先转换为大写
if boFlag then begin
if ch1 in ['a'..'z'] then ch1 := Char (byte (ch1) - $20);
if ch2 in ['a'..'z'] then ch2 := Char (byte (ch2) - $20);
end;
if ch1 <> ch2 then begin
result := n + 1;
break;
end;
end;
end;
function StrCompare(s1:string;s2:string): string;
var
Num,i:Integer;
num1,num2:string;
begin
Result:='';
for i:=1 to Length(s1) do
begin
if (CompareText(S1[i],S2[i])<>0) then
begin
num1:=copy(s1,i,Length(s1)-i+1);
num2:=copy(s2,i,Length(s2)-i+1);
Num:=strtoint(num2)-strtoint(num1) ;
result:=inttostr(Num);
exit ;
end;
end; end;
1:返回值应该为 integer,代码中间也不该使用大量操作 string的方法
2:如果Length (s2) < length (s1) 将会内存访问异常
3: 逻辑也不对
var i,max: integer;
begin
if length(str1) > length(str2) then max:= length(str2) else max:= length(str1);
for i:= 1 to max do
begin
if copy(str1,1,i) <> copy(str2,1,i) then
begin
result:= i;
exit;
end;
end;
end;
var nLen, nLen2, n: integer;
ch1, ch2 :Char;
p1, p2 :PChar;
begin
result := -1;
nLen := length (S1);
nLen2 := length (S2); p1 := PChar (S1); //使用中间变量的目的是为了代码易懂,而且并不会影响速度,因为只有一次mov 操作,不涉及到循环
p2 := PChar (S2); for n := 0 to nLen - 1 do begin
//如果到达比较短的那个字符串的结束位置,则终止循环
if n = nLen2 then begin
result := nLen2;
break;
end;
//为了进行大小写转换,用了个中间变量,保存当前操作的字节,如果不考虑大小写参数,可以通过简单比较S1[n+1]与S2[n+1]进行操作
ch1 := p1[n];
ch2 := p2[n]; //忽略大小写 先转换为大写
if boFlag then begin
if ch1 in ['a'..'z'] then ch1 := Char (byte (ch1) - $20);
if ch2 in ['a'..'z'] then ch2 := Char (byte (ch2) - $20);
end;
if ch1 <> ch2 then begin
result := n + 1;
break;
end;
end;
end;
有问题吗?一个字符串而已,以现在的CPU,这点效率都来算,哈哈比较一个字符串,跟一个字符,没多大分别吧,哈哈而我的代码比你的还短喔~~ 还有,你用了七个变量,我只用了两个变量,唉~~你也何必太较真呢??
AnsiString str1 = "abc123456";
AnsiString str1 = "abc123654";int str_cmp_pos(const AnsiString &str1, const AnsiString &str2)
{
int pos = -1;
int str1_size = str1.Length();
int str2_size = str2.Length();for (int i=1; true; i++)
{
if ((i > str1_size) || (i > str2_size))
break; if (str1[i] != str2[i])
break;
}if ((i > str1_size) && (i > str2_size))
pos = -1;return pos;
}
// 上面的,犯了严重的错误.
//c++方法,c++builder, 相同返回-1
AnsiString str1 = "abc123456";
AnsiString str1 = "abc123654";int str_cmp_pos(const AnsiString &str1, const AnsiString &str2)
{
int pos = -1;
int str1_size = str1.Length();
int str2_size = str2.Length();for (int i=1; true; i++)
{
if ((i > str1_size) || (i > str2_size))
{
pos = i;
break;
} if (str1[i] != str2[i])
{
pos = i;
break;
}
}if ((i > str1_size) && (i > str2_size))
pos = -1;return pos;
}