skip to Main Content

Removing Excess Whitespace from a String

I was looking for the most efficient way to remove excess white space from a string and wrote the following benchmark. Guess which algorithm is faster?

const int iterations = 200000;
const string expr = " Hello    world! Why    are so    many spaces?  Testing One   two three    four    five.";

// Remove excess space using Regex
var doRegex = new Action(() =>
{
    for (int i = 0; i < iterations; i++)
    {
        var newStr = Regex.Replace(expr, @"\s{2,}", " ");
    }
});


// Remove excess space using Split/Join
var doSplit = new Action(() =>
{
    for (int i = 0; i < iterations; i++)
    {
        var newStr = String.Join(" ", expr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
    }
});

var benchMark = new Func<string, Action, long>((name, a) => {
    var sw = Stopwatch.StartNew();
    a();
    sw.Stop();
    Console.WriteLine(name + ": " + sw.ElapsedMilliseconds);
    return sw.ElapsedMilliseconds;
});

// Warming up
Console.WriteLine("Warming up.");
doRegex();
doSplit();

// Run benchmark
long regexElapsed = benchMark("Regex", doRegex);
long splitElapsed = benchMark("Split", doSplit);

On my PC, the Split method is about 7.5 times faster than Regex.

image

Transactional File Manager Version 1.2 .1 Released

Version 1.2 of Transactional File Manager is now available on CodePlex.

Here’s the release notes:

Release Notes

This is a minor bug-fix/minor refactor release.
Fixes:

  • Copy now uses the overwrite parameter correctly.
  • Fixes crashing problem on some machines due to use of GetsequentialGuid.
  • CreateDirectory now supports rolling back nested directories.
  • RollbackFile Rollback() may throw exception when original directory has been deleted.
  • Security issue when writing to event logs.

Enhancements:

  • Upgrade solution/project files to Visual Studio 2010
  • Use NUnit instead of VSTS framework
  • Changes to support Visual Studio Express

Thanks to karbuke and dorong for their contributions.

Getting the Starting Day of the Week for Any Date

From my code snippets, here’s a function that will return the starting day of the week for any date:

/// <summary>
/// Gets the start of the week that contains the specified date.
/// </summary>
/// <param name="date">The date.</param>
/// <param name="weekStartsOn">The day that each week starts on.</param>
/// <returns>The start date of the week.</returns>
public static DateTime GetStartOfWeek(DateTime date, DayOfWeek weekStartsOn)
{
    int days = date.DayOfWeek - weekStartsOn;
    DateTime startOfWeek = days>=0 ? date.AddDays(-days) : date.AddDays(-7 - days);
    return startOfWeek;
}

Transactional File Manager Is Now On CodePlex

It’s my first open source project! I’ve gone open source with my Transactional File Manager. Check out the CodePlex link here.

Use any file system as a transactional file system! Transactional File Manager is a .NET API that supports including file system operations such as file copy, move, delete in a transaction. It’s an implementation of System.Transaction.IEnlistmentNotification (works with System.Transactions.TransactionScope).

image

More on Transactional File Manager in my original blog post on it. If you are interested in contributing to the project, let me know.

MSMQ Installation on Windows Server 2008 Fail with 0x80070643

If you get one of these errors starting MSMQ Server or installing MSMQ on Windows (Server 2008), this article describes a potential solution:

  • You cannot start MSMQ Service with the following entry in the Application Event Log: The Message Queuing service cannot start. The internal private queue ‘admin_queue$’ cannot be initialized. If the problem persists, reinstall Message Queuing. Error 0xc00e0001.
  • You cannot install MSMQ with the following error: Attempt to install Message Queuing Server failed with error code 0x80070643. Fatal error during installation. The following features were not installed: Message Queuing Services/Message Queuing Server.

    MSMQ Installation Result

 

For me, the solution was to delete the Registry key KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSMQ, then try the installation again.

See also:

Back To Top