StringExtension
1.0.3 (2018
This class is a collection of methods (MidB, LenB, etc.) for manipulating strings in bytes in C#/VB. Half-width characters are treated as 1 byte long, and full-width characters are treated as 2 bytes long.
Since it is implemented as an extension method, the readability of the code increases. You can write your code as a method chain.
First, you need to make this extension method available in the using directive.
using StringExtension ;Call the method as follows.
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バイト"With method chaining.
Console . WriteLine ( text . MidB ( 3 , 7 ) . LenB ( ) . ToString ( ) ) ; // 出力: "7" You must first make this extension method available in the Imports statement.
Imports StringExtensionCall the method as follows.
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バイト"With method chaining.
Console.WriteLine(text.MidB( 3 , 7 ).LenB().ToString()) ' 出力: "7" StringExtension.cs or StringExtension.vb from your source code and add it to your project.StringExtension.dll and StringExtension.xml to the references in your project. projects folderStringExtension.sln : Solution implemented in C#.StringExtensionVB.sln : Solution implemented in VB. However, the test code is in C#.src foldertest folderReleased under the MIT license.