New Projects Section and StringDiff

Published: {ts '2011-04-13 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/new-projects-section-and-stringdiff/

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.

Base String: #string1#
Compare String: #string2#
#colorDiff#
#tableDiff#

Base String: #string1#
Compare String: #string2#
#colorDiff#

More Info

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