edeaux
asked on
String comparison
I need a report using html that will show the difference of a sentence to another sentence quickly. The difference (by character) will display in red color so need to add <font tag>. Anybody here knows how to do it?
example:
Right Sentence: The ball is bouncing.
Sentence entered by a user: The ball is bounsing.
So letter "s" in bounsing entereed by a user will be displayed in red.
thanks,
edwin
example:
Right Sentence: The ball is bouncing.
Sentence entered by a user: The ball is bounsing.
So letter "s" in bounsing entereed by a user will be displayed in red.
thanks,
edwin
You could load both sets of data into two stringlists and loop through the strings.
StringList1.LoadFromFile(' c:\somepat h\somefile ')
StringList2.LoadFromFile(' c:\somepat h\somefile ')
Grab each line from the stringlist into 2 temporary strings and then loop through character by character
for i := 0 to Pred(StringList1.Count) do
begin
sTemp1 := StringList1.Strings[i];
sTemp2 := StringList2.Strings[i];
for j := 1 to Length(sTemp1) do
begin
if sTemp1[j] <> sTemp2[j] then
// add your extra font tags
end
end;
This is just a rough guide but should be able to help you on your way I think
StringList1.LoadFromFile('
StringList2.LoadFromFile('
Grab each line from the stringlist into 2 temporary strings and then loop through character by character
for i := 0 to Pred(StringList1.Count) do
begin
sTemp1 := StringList1.Strings[i];
sTemp2 := StringList2.Strings[i];
for j := 1 to Length(sTemp1) do
begin
if sTemp1[j] <> sTemp2[j] then
// add your extra font tags
end
end;
This is just a rough guide but should be able to help you on your way I think
procedure TForm1.Button1Click(Sender : TObject);
var
sC, sW: String;
i : Integer;
begin
sC := 'The ball is bouncing';
sW := 'The ball is bounsing';
for i := 1 to Length(sC) do
begin
if sC[i] <> sW[i] then
begin
// change font color of wrong character
ShowMessage('wrong ' + sW[I]);
end;
end;
end;
HTH
Ashok
var
sC, sW: String;
i : Integer;
begin
sC := 'The ball is bouncing';
sW := 'The ball is bounsing';
for i := 1 to Length(sC) do
begin
if sC[i] <> sW[i] then
begin
// change font color of wrong character
ShowMessage('wrong ' + sW[I]);
end;
end;
end;
HTH
Ashok
You have to also take into consideration, spaces, I guess.
The ball is bouncing
The ball is bouncing
Even if you make the blanks into BOLD or RED, your user can't see it. So I guess it's best you clearly define how are you going to match the differences first :-)
The ball is bouncing
The ball is bouncing
Even if you make the blanks into BOLD or RED, your user can't see it. So I guess it's best you clearly define how are you going to match the differences first :-)
procedure TForm1.Button1Click(Sender : TObject);
var // fuction TForm1.Compare2Strings(sC, sW : String);
sC, sW: String; // you can move these 2 variables in function declaration, and make new function shown in above comment line
i : Integer;
begin
sC := 'The ball is bouncing';
sW := 'The ball is bounsing';
if sC <> sW then
for i := 1 to Length(sC) do
begin
if sC[i] <> sW[i] then
begin
// change font color of wrong character
ShowMessage('wrong ' + sW[I]);
end;
end;
end;
HTH
Ashok
var // fuction TForm1.Compare2Strings(sC,
sC, sW: String; // you can move these 2 variables in function declaration, and make new function shown in above comment line
i : Integer;
begin
sC := 'The ball is bouncing';
sW := 'The ball is bounsing';
if sC <> sW then
for i := 1 to Length(sC) do
begin
if sC[i] <> sW[i] then
begin
// change font color of wrong character
ShowMessage('wrong ' + sW[I]);
end;
end;
end;
HTH
Ashok
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
edeaux,
Has anyone answered you question correctly?
Ashok
Has anyone answered you question correctly?
Ashok
edeaux,
Has anyone answered your question correctly?
Ashok
Has anyone answered your question correctly?
Ashok
Th ball is bouncing
or
The ball is bouncing
or
The ball is dgfdfgdsfgsdf