• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Change Notification

New Here ,
Jan 08, 2007 Jan 08, 2007

Copy link to clipboard

Copied

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

Example:

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.
TOPICS
Advanced techniques

Views

253

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 08, 2007 Jan 08, 2007
Something like this. Not sure what will happen to the spaces but,

<cfscript>
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");
else
x = QuerySetCell(myQuery, "colour", ...

Votes

Translate

Translate
Guest
Jan 08, 2007 Jan 08, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 08, 2007 Jan 08, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 08, 2007 Jan 08, 2007

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 08, 2007 Jan 08, 2007

Copy link to clipboard

Copied

LATEST
Something like this. Not sure what will happen to the spaces but,

<cfscript>
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");
else
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
</cfscript>

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation