4 Replies Latest reply on Jan 8, 2007 1:05 PM by Dan Bracuk

    Change Notification

    capsim1 Level 1
      I have two fields in a database. I want to identify the differences (character by character) and highlight those differences in RED.


      Field 1: My name is Jack

      Field 2: My name is JaccK

      The result here would print "My name is JaccK"; with ONLY one of the "c"s highlighted in RED.

      I have no problem identifying that they are different and highlighting the enitre 2nd output as RED; however, I want to know, character by character, what is the difference.

      Does anyone know how I can do this?

      Thanks, in advance, for shedding some light on this.
        • 1. Change Notification
          jdeline Level 1
          To the human eye, you can see that Field 2 has an extra "c" in it. But if you are a computer, scanning from left to right, character by character, and you come up with a "k" in Field 1 and a "c" in Field 2, what do you do? Is the "k" correct in Field 1 and the "c" in Field 2 displays in red? Or is the second "c" in Field 2 correct and the "k" in Field 1 displays in red?

          For example, what character(s) display in red in the two fields below?

          Field 1: I am a bookkeeper.
          Field 2: I am a bokkeeper.
          • 2. Re: Change Notification
            capsim1 Level 1
            If Field 1 is the base, when I print Field #2, the first "k" (character 10) would print in RED and then "eeper." would also be in RED because they differ from the base (character by character.

            Thanks for your input.
            • 3. Re: Change Notification
              jdeline Level 1
              I missed the fact that there is a "base" field. Why not loop through both strings, comparing characters and keeping track of the position number of those that don't match. When you are finished, you can rebuild your string, use ingthe position numbers tell you where to insert <SPAN STYLE="color:red;"></SPAN>
              • 4. Change Notification
                Dan Bracuk Level 5
                Something like this. Not sure what will happen to the spaces but,

                string1 = "I am not a bookkeeper.";
                string2 = "I am not a bokeeper, nor have I played one on televsion";
                myQuery = QueryNew("letter,colour","varchar,varchar");

                for (i = 1; i lte len(string1); i = i + 1) {
                x = QueryAddRow(myQuery);
                x = QuerySetCell(myQuery, "letter", mid(string2, i, 1));

                if (mid(string1, i, 1) is mid(string2, i, 1)
                x = QuerySetCell(myQuery, "colour", "black");
                x = QuerySetCell(myQuery, "colour", "red");
                } // loop

                if (len(string2) gt len(string1) {
                for (i = len(string1) + 1; i lte len(string2); i = i + 1) {
                x = QueryAddRow(myQuery);
                x = QuerySetCell(myQuery, "letter", mid(string2, i, 1));
                x = QuerySetCell(myQuery, "colour", "red");
                } end loop
                } end if

                <cfoutput query="myQuery">
                <font color="#colour#">#letter#</font>