'===============================================================
'機能 :第1引数で指定した文字列を第2引数で指定したバイト数で切り取った文字を返す。
'引数 :objValue チェック対象文字
' :strRep 切り取るバイト数
'戻り値:切り取った文字列
'===============================================================
Public Function CutByte(ByVal strMoji As String, ByVal intByte As Integer) As String
Dim Length As Integer '文字列もレングス
'空文字に対しては常に空文字を返す
If strMoji = "" Then
Return ""
End If
'Lengthが0か、Start以降のバイト数をオーバーする場合はStart以降の全バイトが指定されたものとみなす。
Dim RestLength As Integer = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(strMoji)
If intByte = 0 OrElse intByte > RestLength Then
intByte = RestLength
End If
'切り抜き
Dim SJIS As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift-JIS")
Dim Byt() As Byte = CType(Array.CreateInstance(GetType(Byte), intByte), Byte())
Array.Copy(SJIS.GetBytes(strMoji), 0, Byt, 0, intByte)
Dim st1 As String = SJIS.GetString(Byt)
'▼切り抜いた結果、最後の1バイトが全角文字の半分だった場合、その半分は切り捨てる。
Dim ResultLength As Integer = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(st1)
If Asc(Strings.Right(st1, 1)) = 0 Then
'VB.NET2002,2003の場合、最後の1バイトが全角の半分の時
Return st1.Substring(0, st1.Length - 1)
ElseIf Length = ResultLength - 1 Then
'VB2005の場合で最後の1バイトが全角の半分の時
Return st1.Substring(0, st1.Length - 1)
Else
'その他の場合
Return st1
End If
End Function