StringExtension
1.0.3 (2018
此类是用于在 C#/VB 中以字节为单位操作字符串的方法(MidB、LenB 等)的集合。半角字符被视为 1 个字节长,全角字符被视为 2 个字节长。
由于它是作为扩展方法实现的,因此代码的可读性增加了。您可以将代码编写为方法链。
首先,您需要在using
指令中使此扩展方法可用。
using StringExtension ;
调用该方法如下。
string text = "半角1バイト/全角2バイト" ;
Console . WriteLine ( $ "text のバイト数は { text . LenB ( ) } " ) ; // 出力: "text のバイト数は 23"
Console . WriteLine ( text . MidB ( 3 , 7 ) ) ; // 出力: "1バイト"
Console . WriteLine ( text . LeftB ( 5 ) ) ; // 出力: "半角1"
Console . WriteLine ( text . RightB ( 11 ) ) ; // 出力: "全角2バイト"
通过方法链。
Console . WriteLine ( text . MidB ( 3 , 7 ) . LenB ( ) . ToString ( ) ) ; // 出力: "7"
您必须首先在Imports
语句中使该扩展方法可用。
Imports StringExtension
调用该方法如下。
Dim text As String = "半角1バイト/全角2バイト"
Console.WriteLine( $ "text のバイト数は {text.LenB()}" ) ' 出力: "text のバイト数は 23"
Console.WriteLine(text.MidB( 3 , 7 )) ' 出力: "1バイト"
Console.WriteLine(text.LeftB( 5 )) ' 出力: "半角1"
Console.WriteLine(text.RightB( 11 )) ' 出力: "全角2バイト"
通过方法链。
Console.WriteLine(text.MidB( 3 , 7 ).LenB().ToString()) ' 出力: "7"
StringExtension.cs
或StringExtension.vb
并将其添加到您的项目中。StringExtension.dll
和StringExtension.xml
添加到项目中的引用中。 projects
夹StringExtension.sln
:用 C# 实现的解决方案。StringExtensionVB.sln
:用 VB 实现的解决方案。不过,测试代码是用 C# 编写的。src
文件夹test
文件夹根据麻省理工学院许可发布。