XNa advanced programming: Xbox 360 and Windows 3-6

Source: Internet
Author: User
3.6 stringhelper Class


StringhelperClass is one of the largest auxiliary classes. It is estimated that it is the first auxiliary class I wrote. Because there are a lot of processing operations on strings, it is easy to think of some methods to improve performance, it is easier to process string lists, output string data, and so on.

Take a lookStringhelperClass Structure (3-9As shown in), you will find that there are many methods, and the overloaded methods support many different types of parameters. It also contains a large number of unit tests, which you have seen before.

Figure3-9

You may ask why there are so many methods in this class, but there are so few unit tests. This is because I started writing this class many years ago, and I didn't start to use unit testing at that time. Some of the methods are. NET 2.0 FrameworkSo they do not make much sense, but I am still used to using my own writing methods. I just hope some of these methods can help you. It takes some time to be familiar with so many methods, but when you find that a method can satisfy your needs for a complex string operation, you may thank me (or, of course, you may want to thank yourself if you have your own helper classes ).

Extract file name

In System. Io The Path Class, including some similar Getdirectory , Cutextension Method, Stringhelper One of the most useful methods for processing file names in a class is Extractfilename It removes the path name and extension of the file, and only the file name is left. Path Class Getfilenamewithoutextension Methods can also be used to perform similar operations, but for some reason I prefer my methods. If you want to implement your own method, you need some practical work.CodeThis will be interesting. Again, you do not have to write it yourself. Path Method that already exists in the class, unless you do not know Framwork Or you want to study it yourself.

I haven't tested it for a long timeStringhelperClass, But I guess most methods process fasterPathClass is much faster.

///   <Summary>
/// Extracts filename from full path + filename, cuts off 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 Unit Tests to this method is also very easy. Use the following code to check whether the output result is correct:

Assert. areequal ( " Somefile " ,
Stringhelper. extractfilename ( " Somedir \ somefile.bmp " ));

Output list


Stringhelper Another special method in the class is Writearraydata , Which includes the image list, array, and Ienumerable Data is output as text strings so that they can be written into log files. Its implementation is also 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)

Both the list and the generic list are from Arraylist Class, so you can pass the dynamic list type to this method. In addition, Array Type, special set type, Byte And Integer Array type and Ienumerable There are also corresponding overload versions for the types, but using a non-reference type overload operation will be faster.


You can use the following code to testWritearraydataMethod:

///   <Summary>
/// Test write array
///   </Summary>
// [Test]
Public   Void Testwritearray ()
{
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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.