Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Today I am enabling a new section of my site to host my own projects. Its a bit rough but I will add to it in time. I am also releasing my first simple project stringDiff.

StringDiff - A Human Friendly String Comparison Function

Recently I have been doing some work on oAuth for connecting to twitter and I found myself needing an easy way to highlight differences between two strings. What I wanted was to be able to input two strings and quickly see if they where different and what those differences where.

After some googling I did not find anything (written in cfml) so I wrote this simple component to do it for me. It worked out so well for me that I am making it available to download for anyone who might like it.

How It Works

StringDiff takes two strings, a base string and a target string. It then compares every character in both strings to determine what changes are needed in the base string to convert it into the target string. The changes are then converted into an array of structs detailing the changes in order of execution.

Example

For example comparing two string "helloworld" and "worldhello" will output an array of the following changes.

  1. New text: "world". i.e. "world" is added to the start of the base string
  2. No change: "hello" i.e. "hello" remains unchanged
  3. Text deleted: "world" i.e. "world" is removed from the end of the string

Additionally to the change array, there are two functions which can be used to format the change array as either a html string which colors changes or a html table.

Usage

To use stringDiff you call one of the three public methods passing in your baseString and targetString like this.

Note: I have included an init function but you can call the functions transiently if you so desire.

<cfset string1="helloworld">
<cfset string2="worldhello">

<!--- Create as object and call --->
<cfset stringDiffObj = createObject("component","model.components.stringDiff").init()>

<cfset colorDiff = stringDiffObj.stringDiffColor(string1,string2)>
<cfset tableDiff = stringDiffObj.stringDiffTable(string1,string2)>
<cfset arrayDiff = stringDiffObj.stringDiffArray(string1,string2)>
            
<p>
Base String: #string1#<br/>
Compare String: #string2#<br/>
#colorDiff#<br/>
#tableDiff#<br/>      
<cfdump var="#arrayDiff#">
</p>

<!--- Alternatively invoke directly like this--->
<cfinvoke
component="stringDiff"
method="stringDiffColor"
baseString="#string1#"
targetString="#string2#"
returnvariable="colorDiff">


<p>
Base String: #string1#<br/>
Compare String: #string2#<br/>
#colorDiff#
</p>

More Info

To learn more about stringDiff visit the projects page to demo or download it.

What Do You Think?

Reader Comments

Post a Comment

Comment Etiquette:

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain or appear to be advertisments, will not be published.
  • Comments that appear to be created for the purpose of linkbuilding to commercial sites will be removed.

We are all adults here so play nice.

*
*



Archives Blog Listing

Tag Listing

Learn CF In A Week

Treehouse

 
Fork me on GitHub