[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

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
0
edeaux
Asked:
edeaux
1 Solution
 
mokuleCommented:
And should be what in
Th ball is bouncing
or
The    ball    is bouncing
or
The ball is dgfdfgdsfgsdf
0
 
mikelittlewoodCommented:
You could load both sets of data into two stringlists and loop through the strings.

StringList1.LoadFromFile('c:\somepath\somefile')
StringList2.LoadFromFile('c:\somepath\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
0
 
AshokCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DragonSlayerCommented:
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 :-)
0
 
AshokCommented:
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
0
 
Slick812Commented:
hello edeaux, Here is some code that will do several things to the Test and Compare string, it will remove the HTML tags, it will chop all series of space charaters to only one space charater, and then add an HTML tag for a red background to any differences between the two text strings
If you do not need the removal of HTML tags then comment out the -
//UserStr := removeTags(UserStr);
//RefStr := removeTags(RefStr);

if you do not need the spaces to be reduced to one the comment out the -
//UserStr := chopSpaces(UserStr);
//RefStr := chopSpaces(RefStr);


here is the code, there is a button click event for the last procedure -



function removeTags(const HasTags: String): String;
var
pT1, pT2, pO: PChar;
begin
Result := HasTags;
if Length(HasTags) < 2 then Exit;
pT1 := PChar(Result);
pT2 := StrEnd(PChar(Result));
repeat
  pT1 := StrScan(pT1, '<');
  if pT1 = nil then Break;
  pO := PChar(Result);
  pT2 := StrScan(pT1, '>');
  if pT2 = nil then Break;
  Delete(Result, pT1 - PChar(Result) +1, pT2 - pT1+1);
  pT1 := PChar(Result)+(pT1-pO);
  until pT2 >= StrEnd(PChar(Result));
end;



function chopSpaces(const HasSpaces: String): String;
var
pT1, pT2, pO: PChar;
begin
Result := HasSpaces;
if Length(HasSpaces) < 2 then Exit;
pT1 := PChar(Result);
while pT1 < StrEnd(PChar(Result)) do
  begin
  pT1 := StrScan(pT1, ' ');
  if pT1 = nil then Break;
  pO := PChar(Result);
  pT2 := pT1+1;
  while pT2^ = ' ' do
    Inc(pT2);
  if pT1+1 <> pT2 then
    begin
    Delete(Result, pT1 - pO +1, pT2 - pT1-1);
    pT1 := PChar(Result)+(pT1-pO)+1;
    end else PT1 := pT2;
  end;
end;



function addTags(UserStr, RefStr: String): String;
const
//preTag: String = '<font color="red">';
// place whatever tag code you want to add in these preTag and endTag const
preTag: String = '<font STYLE="background: #FF3333">';
  // background color will also show non matching spaces
endTag: String = '</font>';

var
i, Len, Un1, Un2, preLen, offLen, offSet: Integer;
begin
preLen := Length(preTag)+1;
offLen := preLen+Length(endTag)-1;

UserStr := removeTags(UserStr);
UserStr := chopSpaces(UserStr);

RefStr := removeTags(RefStr);
RefStr := chopSpaces(RefStr);

for i := Length(RefStr) to Length(UserStr)-1 do
  RefStr := RefStr+#1;

Un1 := 0;
Un2 := 0;
offSet := 0;
Result := UserStr;
Len := Length(UserStr);

for i := 1 to Len do
  if UserStr[i] <> RefStr[i] then
    begin
    if Un1 = 0 then
      begin
      Un1 := i;
      if i = Len then
        begin
        Insert(preTag,Result,Un1+offSet);
        Insert(endTag,Result,Len+preLen+offSet);
        end;
      end else
      begin
      Un2 := i;
      if i = Len then
        begin
        Insert(preTag,Result,Un1+offSet);
        Insert(endTag,Result,Len+preLen+offSet);
        end;
      end;
    end else
    begin
    if Un1 = 0 then Continue;
    Insert(preTag,Result,Un1+offSet);
    if Un2 = 0 then
      Insert(endTag,Result,Un1+preLen+offSet)
      else
      Insert(endTag,Result,Un2+prelen+offSet);
    Inc(offset,offLen);
    Un1 := 0;
    Un2 := 0;
    end;
end;

procedure TForm4.but_ColorHtmlClick(Sender: TObject);
var
User, Existing, Output: String;

begin
// button click event to test it
User := 'He r   is  <pre>emoS</pre>   TesP extra';
Existing := 'Here is Some <b>Text<b>';

OutPut := addTags(User, Existing);
ShowMessage(Output);
end;

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

this works for me, . . . .
ask questions if you need more information!
0
 
AshokCommented:
edeaux,

Has anyone answered you question correctly?

Ashok
0
 
AshokCommented:
edeaux,

Has anyone answered your question correctly?

Ashok
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now