I am starting to like C#’s versatility

I have been coding small utility for my own use. This utility is about parsing rss feeds and juggling with files. Names of the files come from feed, so I may get anything. This project is also a learning project for C# and .NET, so it is not always clear how to achieve certain outcomes.

A problem I faced with file names is that they can contain illegal characters and therefore I would have to strip or replace illegal characters from the original string. How can this be done using C#? The answer is Regular expressions. Just by looking the example below you will see how versatile C# really is.

[scode]
public static string ValidFilenameFromString(string name)
{
string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
string invalidRegexpStr = string.Format(@”[{0}]+”, invalidChars);
return Regex.Replace(name, invalidRegexpStr, “_”);
}
[/scode]

One thought on “I am starting to like C#’s versatility”

  1. Only if I had something like this back in the days when I was coding similar gadget with Lingo. Back then there was no rss… My goal was to find information from internet about certain forthcoming radio shows. I did not know exactly what I was looking for nor did I know where to look from. All I knew was that when the information was published it probably contained the words “radio” and “theatre”, in finnish of course =D

    The utility I ended up with read the national radio station’s (YLE) “unpublished” web page and provided a link list with all the entries and links that contained my keywords. The unpublished page I referred to was plain html data and used like a feed by YLE themselves to create more sophisticated program schedules etc.

    It was a pain to parse the entries I got. I had to somehow dig in to that person’s head who wrote all the schedules. I never knew if “the rules” would stay the same and had to prepare all sort of things.

    Another similar project I had when we tried to create a curse filter for school kids to a multimedia game for Finnish national bank, but that is another story =D

Leave a Reply