New Projects Section and StringDiff

Author: Steven Neiland

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.


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.


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)>
Base String: #string1#<br/>
Compare String: #string2#<br/>
<cfdump var="#arrayDiff#">

<!--- Alternatively invoke directly like this--->

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

More Info

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

Reader Comments

  • 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 advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing