3.6 StringHelper类
StringHelper 类是最大的辅助类之一,估计它是我写的第一个辅助类,因为针对字符串的处理操作非常多,很容易就可以想到一些方法来改善性能,更容易地处理字符串列表,输出字符串数据等等。
看一下 StringHelper 类的结构图(如图 3-9 所示),您会发现它的方法非常多,重载的方法支持很多不同类型的参数。它还包含大量的单元测试,前面您就曾见过一个。
图3-9
您可能会问为什么这个类中的方法那么多,而单元测试却那么少。这是因为很多年前我就开始写这个类了,而那个时候还没开始使用单元测试。其中有些方法现在在 .Net 2.0 Framework 中已经被实现了,所以它们也就没有太大的意义,不过我还是习惯于使用自己写的方法,我只是希望其中的某些方法对您能有所帮助。要想熟练使用这么多的方法的确需要一些时间,不过当您发现某个方法可以满足您进行一项复杂的字符串操作时,您或许会感谢我(当然也可能是感谢您自己,如果您有自己的辅助类的话)。提取文件名
在 System.IO 命名空间的 Path 类中,也包含一些类似 GetDirectory 、 CutExtension 的方法,不过 StringHelper 类中用来处理文件名的最有用的方法之一就是 ExtractFilename ,它去掉了文件的路径名和扩展名,仅仅剩下文件的名字。 Path 类中的 GetFileNameWithoutExtension 方法也可以做类似的操作,不过出于某些原因我还是更喜欢自己的方法。如果您想实现自己的方法,并需要一些实际的工作代码,这会很有趣。再强调一次:您不必自己写 Path 类中已经存在的方法,除非您不知道 Framwork 中已经提供了,或者您想自己去研究一下。
我已经很久没有测试 StringHelper 类中方法的处理性能了,不过我猜大多数方法的处理速度要比 Path 类中的快得多。
///
<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)
/// 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)
给这样的方法写单元测试也很简单,使用下面的代码来检查输出的结果是否在正确:
Assert.AreEqual(
"
SomeFile
"
,
StringHelper.ExtractFilename( " SomeDir\\SomeFile.bmp " ));
StringHelper.ExtractFilename( " SomeDir\\SomeFile.bmp " ));
输出列表
StringHelper 类中另一个比较特殊的方法是 WriteArrayData ,它把像列表、数组以及 IEnumerable 数据输出为文本字符串,这样它们就可以被写入日志文件中。它的实现也非常简单:
///
<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)
/// 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)
列表和泛型列表都是从 ArrayList 类继承而来的,所以可以给这个方法传递动态列表类型。另外,对于 Array 类型、特殊的集合类型、 byte 和 integer 数组类型以及 IEnumerable 类型也都存在对应的重载版本,不过使用非引用类型的重载操作速度会更快。
可以使用下面的代码来测试 WriteArrayData 方法:
///
<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()
/// 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()