Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?
Save up to 50% on our desktop apps during our Year End Sale!Save up to 50% on our desktop apps during our Year End Sale, including DisplayFusion, ClipboardFusion, FileSeek, LogFusion, TrayStatus, and VoiceBot!Save up to 50% on our desktop apps during our Year End Sale!

Perform a Word Count on the Copied Text

Description
Takes the copied clipboard text and perform a word count (among other things). A message box is displayed showing the statistics for your copied text.
Language
C#.net
Minimum Version
Created By
Splat
Contributors
-
Date Created
Aug 19, 2014
Date Last Modified
Aug 19, 2014

Macro Code

using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Windows.Forms;

public static class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        // Name:                Word Count
        // Description:    Pops up a message box that shows some statistics about the text on the clipboard, such as word count, number of characters, number of lines, etc.


        // which message layout do we want to display?
        // feel free to add your own layouts below
        int intLayout = 1;    // possible values are 1 or 2

        // create an array of our objects
        // you can add more here as needed, but there may be limited space to display the results in the message box that pops
        objWordCount[] arrWordCount = new objWordCount[2];
        arrWordCount[0] = new objWordCount("Text", text);
        arrWordCount[1] = new objWordCount("No HTML Tags", HttpUtility.HtmlDecode(Regex.Replace(text, @"<[\w\W]+?>", string.Empty)));

        // string used to store our word count message
        string strMessage = string.Empty;

        // different layouts can be added here
        // depending on your OS, you may have to tweak the layouts a little bit
        switch(intLayout)
        {
            default:

                // this is the default layout

                // create our word count message
                for(int i = 0; i < arrWordCount.Length; i++)
                {
                    strMessage +=
                        (arrWordCount[i].title.ToUpper() + " ").PadRight(50, '\x2013') + "\r\n" +
                        "Words " + ("  " + arrWordCount[i].words.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters " + ("  " + arrWordCount[i].chars.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters (no spaces) " + ("  " + arrWordCount[i].charsNoSpaces.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters (no breaks) " + ("  " + arrWordCount[i].charsNoBreaks.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters (no breaks or spaces) " + ("  " + arrWordCount[i].charsNoSpacesBreaks.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Lines " + ("  " + arrWordCount[i].lines.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Average characters per word \x2009" + ("  " + (String.Format("{0:0.0}",((double)arrWordCount[i].charsNoSpacesBreaks / (double)arrWordCount[i].words)))).PadLeft(12, '_') + "\r\n" +
                        "Average characters per line \x2009" + ("  " + (String.Format("{0:0.0}",((double)arrWordCount[i].charsNoBreaks / (double)arrWordCount[i].lines)))).PadLeft(12, '_') + "\r\n" +
                        "Average words per line \x2009" + ("  " + (String.Format("{0:0.0}",((double)arrWordCount[i].words / (double)arrWordCount[i].lines)))).PadLeft(12, '_') + "\r\n" +
                        "\r\n\r\n";
                }

                break;

            case 2:

                // this layout has a problem if there are more then two word count objects, use the default layout instead

                // set the length of our titles to a maximum of 12 characters (we can't have them being too long in this layout)
                for(int i = 0; i < arrWordCount.Length; i++)
                {
                    if(arrWordCount[i].title.Length > 12) arrWordCount[i].title = arrWordCount[i].title.Substring(0, 12);
                }

                // create our word count message
                strMessage = string.Empty;
                strMessage += "Clipboard Word Count";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (arrWordCount[i].title).PadLeft(12, '\x2002');
                strMessage += "\r\n" + new string('\x203E', 60) + "\r\nWords";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].words.ToString()).PadLeft(12, '_');
                strMessage += "\r\n\r\nCharacters";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].chars.ToString()).PadLeft(12, '_');
                strMessage += "\r\nCharacters (no spaces)";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].charsNoSpaces.ToString()).PadLeft(12, '_');
                strMessage += "\r\nCharacters (no breaks)";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].charsNoBreaks.ToString()).PadLeft(12, '_');
                strMessage += "\r\nCharacters (no spaces or breaks)";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].charsNoSpacesBreaks.ToString()).PadLeft(12, '_');
                strMessage += "\r\n\r\nLines";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].lines.ToString()).PadLeft(12, '_');
                strMessage += "\r\n\r\nAverage characters per word";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + "\x2009" + (" " + String.Format("{0:0.0}",((double)arrWordCount[i].charsNoSpacesBreaks / (double)arrWordCount[i].words))).PadLeft(12, '_');
                strMessage += "\r\nAverage characters per line";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + "\x2009" + (" " + String.Format("{0:0.0}",((double)arrWordCount[i].charsNoBreaks / (double)arrWordCount[i].lines))).PadLeft(12, '_');
                strMessage += "\r\nAverage words per line";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + "\x2009" + (" " + String.Format("{0:0.0}",((double)arrWordCount[i].words /(double)arrWordCount[i].lines))).PadLeft(12, '_');

                break;
        }

        // display our word count message box
        MessageBox.Show (
            strMessage.Trim(),
            "Clipboard Word Count".PadRight(100, '\x2002'),
            MessageBoxButtons.OK,
            MessageBoxIcon.None,
            MessageBoxDefaultButton.Button1,
            MessageBoxOptions.RightAlign
            );

        // we don't want to alter the original text on the clipboard, so return the same text as we received
        return text;
    }

}

public class objWordCount
{
    public string title;
    public int pages;
    public int words;
    public int chars;
    public int charsNoSpaces;
    public int charsNoBreaks;
    public int charsNoSpacesBreaks;
    public int lines;

    public objWordCount(string strTitle, string strInput)
    {
        title               = strTitle;
        pages               = new Regex(@"\f", RegexOptions.Multiline).Matches(strInput).Count + 1;  // based on form feed characters, not very common these days
        words               = new Regex(@"\b\w+\b", RegexOptions.Multiline).Matches(strInput).Count;
        chars               = strInput.Length;
        charsNoSpaces       = Regex.Replace(strInput, @"[ \t\v]", string.Empty).Length;
        charsNoBreaks       = Regex.Replace(strInput, @"[\f\r\n]", string.Empty).Length;
        charsNoSpacesBreaks = Regex.Replace(strInput, @"[\s]", string.Empty).Length;
        lines               = new Regex(@"[\n\r]{1,2}", RegexOptions.Multiline).Matches(strInput).Count + 1;
    }
}