Fórum Comparando Strings... #229367
02/05/2004
0
Str1 = Str2, caso sensitivo, tb tem AnsiCompareStr(Str1, Str2)
AnsiCompareText(Str1, Str2), caso insensitivo
Ex:
if AnsiCompareText(Str1, Str2) = 0 then
Strings_Iguais();
Mas, alguem poderia me explicar detalhadamente como eu uso?
Jasig Aurumalfa
Curtir tópico
+ 0Posts
03/05/2004
Lucas Silva
// vamos supor que você quer comparar duas strings uma que esta no edt1 e outra no edt2; if AnsiCompareText(edt1.text, edt2.text) = 0 then // se for igual a zero é porque elas são iguais... begin ShowMessage(´Estas Strings são iguais.´); // aqui você coloca o código que quiser. end;
Gostei + 0
03/05/2004
Nildo
if UpperCase( string1 ) = UpperCase( String2 ) then // São iguais
Gostei + 0
03/05/2004
Marco Salles
Método Do lucas:
// vamos supor que você quer comparar duas strings uma que esta no edt1 e outra no edt2; if AnsiCompareText(edt1.text, edt2.text) = 0 then // se for igual a zero é porque elas são iguais... begin ShowMessage(´Estas Strings são iguais.´); // aqui você coloca o código que quiser. end;
Método do nildo:
if UpperCase(edit1.text) = UpperCase(edit2.text) then // São iguais
Por outro lado se voce por alguma razão se [b:622e9f3a48]preocupar[/b:622e9f3a48] com Maisculas/Minusculas.. Faça simplismente o teste
procedure TForm1.Button1Click(Sender: TObject); begin if (edit1.text) =(edit2.text) then begin showmessage(´Sao Iguais´); end else showmessage(´Sao Diferentes´); end;
Gostei + 0
11/05/2017
Carlos Eduardo
unit uClass.ComparandoStrings;
interface
// Parte do Codigo foi pesquisado
uses
math; {********************************************** NAO ESQUEÇE O USES **********************************************}
type
// Acho desnecessario mas fiz XD
TResult = record
Porcentagem : Double;
Mudancas : Integer;
end;
TComparaçao = class
Strict private
// retorna a quantidade de alteraçoes necessarias
function qtdAlteracao(Str1,Str2 : String):Integer;
// retorna a porcentagem de semelhança
function Porcentagem(Str1,Str2 : String):Double;
public
// metodo publico que retorna o resultado das duas String
function CompareStr(Str1,Str2 : String):TResult;
end;
implementation
{ TComparaçao }
function TComparaçao.qtdAlteracao(Str1, Str2: String): Integer;
var
d: array of array of integer;
i, j, cost: integer;
begin
SetLength(d, Length(str1) + 1);
for i := Low(d) to High(d) do
begin
SetLength(d[i], Length(str2) + 1);
end;
for i := Low(d) to High(d) do
begin
d[i, 0] := i;
for j := Low(d[i]) to High(d[i]) do
begin
d[0, j] := j;
end;
end;
for i := Low(d) + 1 to High(d) do
for j := Low(d[i]) + 1 to High(d[i]) do
begin
if str1[i] = str2[j] then
cost := 0
else
cost := 1;
d[i, j] := Min(Min( d[i - 1, j] + 1,{deleção}
d[i, j - 1] + 1),
d[i - 1, j - 1]+cost{Substituiçao});
end;
Result := d[Length(str1), Length(str2)];
end;
function TComparaçao.porcentagem(Str1, Str2: String): Double;
var
i, iMin, iMax, iSameCount: Integer;
begin
iMax := Max(Length(Str1), Length(Str2));
iMin := Min(Length(Str1), Length(Str2));
iSameCount := -1;
for i := 0 to iMax do
begin
if i > iMin then
break;
if Str1[i] = Str2[i] then
Inc(iSameCount)
else
break;
end;
if iSameCount > 0 then
Result := (iSameCount / iMax) * 100
else
Result := 0.00;
end;
function TComparaçao.CompareStr(Str1, Str2: String): TResult;
Var
Res : TResult;
begin
Res.Porcentagem := Porcentagem(Str1,Str2);
Res.Mudancas := qtdAlteracao(Str1,Str2);
CompareStr := Res;
end;
end.
Gostei + 0
12/05/2017
Edson Sobrinho
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)