Stringhelper class
The stringhelper class is one of the largest helper classes. It is estimated that it is the first helper class I wrote. Because processing strings involves so many problems, it is easy to think of many methods to improve performance, it is easier to process string lists, output string data, and so on.
If you look at the stringhelper class (as shown in 3-9), you will immediately find many methods, and all overloaded methods support many different parameter types. It also contains a considerable number of unit tests. A few minutes ago, you saw a unit test from the stringhelper class.
Figure 3-9
You may say to yourself why there are so few unit tests and so many methods in this class. This is because I started writing this class many years ago, far before I started using unit testing. Some of these methods do not make much sense in. NET 2.0, because now the framework implements them, but I am still used to my own methods. I just hope you can find some useful methods in this class. It may take some time to get used to so many methods, but when you need a complex string operation, you will thank me for this useful method (if you have your own helper class, you are your own ).
Extract file name
In system. the path class of the IO namespace also contains many methods such as getdirectory and cutextension. However, one of the most useful methods used to process file names in the stringhelper class is the extractfilename method, it removes both the path and extension name, and only obtains the file name, no other.
The path. getfilenamewithoutextension method does something similar, but for some reason I prefer my method. If you want to implement your own method, and you need to be able to work on itCodeAnd may be interesting. Once again, you do not have to write your own path method, but sometimes you do not know what framwork provides, or you just want to study it on your own.
It has been a long time since I tested the performance of these methods, but I still think that most stringhelper classes are faster than some path classes.
/// <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 "));
Output list
Another unique method in the stringhelper class is the writearraydata method, which writes list, array, or ienumerable data of any type as text strings that can be written into log files. The implementation is still very 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)
List, or even generic list are inherited from the arraylist class, so you can pass any dynamic list to this method. For array arrays, special sets, byte, and integer arrays with special overloaded versions, these types that work with the ienumerable interface also have corresponding overloaded versions, however, using a non-object type overload will be faster.
You can write the following code to test the writearraydata method:
/// /// test writearraydata // [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
genericlist = new list
(); genericlist. add ("whats"); genericlist. addrange (New String [] {"going", "on"}); assert. areequal ("whats, going, on", writearraydata (genericlist);} // testwritearray ()