String.WordCount extension method

I’m in the final steps of a project where we needed a word count method. With some googling I found a good foundation from microsoft.com MSDN pages. I need some customizations and made an additional overrided method where I could add a string with delimiter characters, instead of char array.

    public static class MyExtension
    {
        public static int WordCount(this String str)
        {
            var splitCharts = " .?-@";
            return WordCount(str, splitCharts);
        }

        public static int WordCount(this String str, string delimiters)
        {
            var opt = StringSplitOptions.RemoveEmptyEntries;
            return str.Split(delimiters.ToArray(), opt).Length;
        }
    }

The use of the extension method is like this:

string str = "HERØY-I-MØRE-OG-ROMSDAL";
var count = str.WordCount("-");

Will result in count being 5, since delimiter is character ‘-‘.

Advertisements

C# and Capitalizing string

The class string in C# has methods for ToLower(), ToUpper(), but not for ToCapitalize() og something like that. When I started to learn C# some years back that was something that surprised me alot – and it still do. The method for capitalizing strings, is called ToTitleCase and is located in the TextInfo class. The best way to capitalize string id to instansiate a CultureInfo object, retrieve the TextInfo instans and call the ToTitleCase method with the string.

        public static string ToInitialCase(string str)
        {
            var textInfo = new CultureInfo("nb-NO", false).TextInfo;
            string tmp = textInfo.ToTitleCase(str.ToLower());
            return tmp;
        }

Be aware of the ToLower call. I’m not sure if this is a bug, but the capitalizing didn’t correctly my norwegian letters before I had the ToLower() call.

IIS Logfile analyser

Recently, I needed a tool for analysing the web site traffic for a customer. Microsoft IIS isn’t my main working area, so I started googling for free tools available. I came accross utilities from commecial companies like webtrend, but that wasn’t what I was looking for.

I found a great blog post from carlosag that describes a Microsoft utility called Log Parser that has a SQL-like command-line interface. Just something for me 🙂

I create a file traffic_2011_March.sql with the following statement to determine hourly hit calculations for march month.

SELECT 
    QUANTIZE(TO_LOCALTIME(TO_TIMESTAMP(date, time)), 3600) AS Hour, 
    COUNT(*) AS Hits 
FROM c:\inetpub\logs\LogFiles\W3SVC1\* 
WHERE date >='2011-03-01' and date < '2011-04-01' 
Group By Hour

Opened a CMD window and located LogParser directory “C:\Program Files (x86)\Log Parser 2.2”, and ran the command.

LogParser.exe -i:W3C file:traffic_2011_March.sql -o:CSV

Visit Log Parser community forum.

Installing server-side “Web Site Administration Tool”

When you use Membership provider for user authorization in your .NET project, you have access to the Web Site Administration Tool (WSAT) from the Visual Studio IDE. But when you have deployed the web application to a test and/or production server, the WSAT web client can’t be used to directly maintaining the membership tables in the test and/or production enviroment.

Off course, you can maintain the membership provider tables directly in SQL Management Studio and do the SELECTs and  UPDATEs yourself, but there are an easier way of doing this.

I found this nice solution by Rahul Soni today on how to deploy the Web Site Administration Tool server-side