StringHelper Class


The StringHelper class is one of the biggest helper classes and I guess it was the first helper class I ever wrote because working with strings involves so many things, it is very easy to think about many ways to improve performance, handle lists of strings easier, output string data easily, and so on.

If you take a look at the StringHelper class (see Figure 3-9) you will immediately notice the many methods and all the method overloads supporting many different parameter types. It also contains quite a lot of unit tests; you just saw a unit test from the StringHelper class a few minutes ago.

image from book
Figure 3-9

You might ask yourself why there are only a couple of unit tests, but so very many methods in this class. The reason for this is that I started coding this class many years ago, a long time before I started using unit testing. Some methods don’t make much sense in .NET 2.0 because the framework implements them now, but I got used to my own methods. I just hope you can find some useful methods in this class. It might take a while to get used to the many methods, but when you need a complicated string operation you will thank me (or yourself if you have your own helper class) for a useful method.

Extracting Filenames

Many of the GetDirectory, CutExtension, and so on methods are available in the Path class from the System.IO namespace too, but one of the most useful methods in StringHelper for filenames is the ExtractFilename method, which cuts off both the path and the extension to just get the name of a file, nothing else. Path.GetFileNameWithoutExtension does a similar thing, but I like my own method better for some reason. It might also be interesting if you want to implement your own methods and need some working code you can start with. Again: You don’t have to write your own Path methods, but sometimes you don’t know what the framework provides or you just want to investigate yourself.

It’s been a long time since I tested the performance of these methods, but I would still guess that most of the StringHelper methods are faster than some of the Path methods.

  /// <summary> /// Extracts filename from full path+filename, cuts of extension /// if cutExtension is true. Can be also used to cut of directories /// from a path (only last one will remain). /// </summary> static public string ExtractFilename(string pathFile, bool cutExtension) {   if (pathFile == null)     return "";   // Support windows and unix slashes   string[] fileName = pathFile.Split(new char[] { '\\', '/' });   if (fileName.Length == 0)   {     if (cutExtension)       return CutExtension(pathFile);     return pathFile;   } // if (fileName.Length)   if (cutExtension)     return CutExtension(fileName[fileName.Length - 1]);   return fileName[fileName.Length - 1]; } // ExtractFilename(pathFile, cutExtension) 

Writing a unit test for a method like this is also very simple. Just check if the expected result is returned:

  Assert.AreEqual("SomeFile",   StringHelper.ExtractFilename("SomeDir\\SomeFile.bmp")); 

Writing Lists

A little bit more unique is the WriteArrayData method in the StringHelper class, which writes any type of lists, arrays, or IEnumerable data to a text string, which then can be used for logging. The implementation is again quite simple:

  /// <summary> /// Returns a string with the array data, ArrayList version. /// </summary> static public string WriteArrayData(ArrayList array) {   StringBuilder ret = new StringBuilder();   if (array != null)     foreach (object obj in array)       ret.Append((ret.Length == 0 ? "" : ", ") +         obj.ToString());   return ret.ToString(); } // WriteArrayData(array) 

Lists, even generic ones, are derived from the ArrayList class and therefore you can call this method with any dynamic list. For arrays a special overload exists, also for special collections and byte or integer arrays, which would work with IEnumerable too, but it is faster to use overloads that don’t use the object class.

To test the WriteArrayData method you could write a method like the following one:

  /// <summary> /// Test WriteArrayData /// </summary> [Test] public void TestWriteArrayData() {   Assert.AreEqual("3, 5, 10",     WriteArrayData(new int[] { 3, 5, 10 }));   Assert.AreEqual("one, after, another",     WriteArrayData(new string[] { "one", "after", "another" }));   List<string> genericList = new List<string>();     genericList.Add("whats");     genericList.AddRange(new string[] { "going", "on" });   Assert.AreEqual("whats, going, on",     WriteArrayData(genericList)); } // TestWriteArray() 




Professional XNA Game Programming
Professional XNA Programming: Building Games for Xbox 360 and Windows with XNA Game Studio 2.0
ISBN: 0470261285
EAN: 2147483647
Year: 2007
Pages: 138

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net