Sharepoint Insight by Namwar Rizvi

Sharepoint Tips, Tricks and inside knowledge from real world experience by Namwar Rizvi

SPDiffUtility: An excellent class to find differences in strings with HTML highlighting

Posted by namwar on August 25, 2008


While exploring Sharepoint utility classes, I found an excellent class which deserve more credit then it currently enjoys. It is SPDiffUtility. This class has only one static method called Diff which determines the text differences between two strings. It takes three parameters:

  1. str1: The original string
  2. str2: The changed string
  3. maxDifferences: A 32-bit integer representing the maximum number of differences to find.

The beauty of this function lies in its return value which is a merged version of the specified strings that contain HTML tags indicating inserted, deleted, or changed text.

This returned string contains HTML tags to highlight the changes, insertions and deletions. The best part is that you can set these tags with your HTML tags if you want. Following is the sample usage of this class.

  1. Create a web site project in Viusal Studio
  2. Add reference to Microsoft.Sharepoint.dll found in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll
  3. Paste the following code in Default.aspx
  4. <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
    <html xmlns=”http://www.w3.org/1999/xhtml”&gt;

    <head runat=”server”>
    <title>Untitled Page</title>
    </head>

    <style type=”text/css”>
    .ms-diffdeletenostrike {
    BACKGROUND-COLOR: #e0e0e0;
    }
    .ms-diffdelete {
    BACKGROUND-COLOR: #e0e0e0;
    }
    .ms-diffdelete {
    TEXT-DECORATION: line-through;
    }
    .ms-diffinsert {
    BORDER-RIGHT: #ffffff 1px solid;
    BORDER-TOP: #ffffff 1px solid;
    = BORDER-LEFT: #ffffff 1px solid;
    BORDER-BOTTOM: #ffffff 1px solid;
    = BACKGROUND-COLOR: #ffeaad;
    }
    </style>
    <body>

    <form id=”form1″ runat=”server”>
    <div>
    <asp:Label ID=”lblResult” runat=”server” Text=””></asp:Label>
    </div>
    </form>

    </body>

    </html>


  5. Open Default.aspx.cs and paste the following code

  6. using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using Microsoft.SharePoint.Utilities;
    using System.Diagnostics;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    string firstString = “This is an initial string”;
    string secondString = “This”;

    //Set the tags
    SPDiffUtility.ChangeOpenTag = ““;
    SPDiffUtility.ChangeCloseTag = “
    “;
    SPDiffUtility.InsertOpenTag = ““;
    SPDiffUtility.InsertCloseTag = “
    “;
    SPDiffUtility.DeleteOpenTag = ““;
    SPDiffUtility.DeleteCloseTag = “
    “;

    //end setting tags

    //Find the difference
    string diffString = SPDiffUtility.Diff(firstString, secondString, Int32.MaxValue);
    lblResult.Text = diffString;

    }
    }

  7. Run web application by pressing Ctrl+F5

You will see “This is an intial string where striked out letters are those which have been deleted in the second string.

Advertisements

2 Responses to “SPDiffUtility: An excellent class to find differences in strings with HTML highlighting”

  1. jaideep said

    really good way to see the difference. whatif we want to have similar functionality in normal c# code. (not in a sharepoint project)

  2. kevin said

    jaideep:
    aaahh….*caugh*reflector*caught*

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: