2013年01月13日

バイト数を用いる文字列関数(改造版)

バイト数を用いる文字列関数 に関し、全角途中の指定時に取得した文字列に文字化けが発生するという指摘があったので、全角途中を指定された場合に半角スペースを挿入するように改造してみました。


VB や VBA の文字列関数である Left、Len、Mid、Right は全角文字でも半角文字でも 1文字と計算されてしまいます。

昔のように(?)、半角は 1バイト、全角は 2バイトとなる関数が欲しいと思ったことはないでしょうか?

例えば、全半角混在の固定長テキストファイルからある一部分(mバイト目から長さ nバイト)を切り出すようなことってありませんか?

通常の Left、Len、Mid、Right のような感覚で使える関数が欲しいと思って作成しました。

ascii_string2.txt






' @(f)
'
' 機能 : ASCIIでのLeftB
'
' 返り値 : 文字列
'
' 引き数 : ARG1 - 文字列(Unicode)
' ARG2 - (ASCIIでの)文字列長
'
' 機能説明 : 文字列の左端からASCIIコードでのバイト数分の文字列を返す。
'
' 備考 :
'
Public Function pAsciiLeftB(ByVal strValue As String, _
ByVal intLength As Integer) As String
Dim intValueLength As Integer

intValueLength = pAsciiLenB(strValue)

If intLength <= 0 Then
pAsciiLeftB = ""
ElseIf intValueLength > intLength Then
pAsciiLeftB = pAsciiMidB(strValue, 1, intLength)
Else
pAsciiLeftB = strValue
End If
End Function

' @(f)
'
' 機能 : ASCIIバイト長取得
'
' 返り値 : バイト長
'
' 引き数 : ARG1 - 文字列(Unicode)
'
' 機能説明 : ASCIIコードでのバイト長を返す。
'
' 備考 :
'
Public Function pAsciiLenB(ByVal strValue As String) As Integer
pAsciiLenB = LenB(StrConv(strValue, vbFromUnicode))
End Function

' @(f)
'
' 機能 : ASCIIでのMidB
'
' 返り値 : 文字列
'
' 引き数 : ARG1 - 文字列(Unicode)
' ARG2 - (ASCIIでの)開始位置
' ARG3 - (ASCIIでの)文字列長(省略可)
'
' 機能説明 : 文字列のASCIIコードでの開始位置からバイト数分の文字列を返す。
'
' 備考 :
'
Public Function pAsciiMidB(ByVal strValue As String, _
ByVal intStart As Integer, _
Optional ByVal varLength As Variant) As String
Dim intValueLength As Integer
Dim intMaxLength As Integer
Dim strRet As String
Dim i As Integer
Dim intLeftlength As Integer
Dim strChar1 As String

intValueLength = pAsciiLenB(strValue)

If intValueLength >= intStart Then
intMaxLength = intValueLength - intStart + 1
If IsMissing(varLength) Then
varLength = intMaxLength
ElseIf varLength > intMaxLength Then
varLength = intMaxLength
ElseIf varLength <= 0 Then
pAsciiMidB = ""
Exit Function
End If
strRet = ""
For i = 1 To Len(strValue)
intLeftlength = pAsciiLenB(Left(strValue, i))
If intLeftlength >= intStart Then
strChar1 = Mid(strValue, i, 1)
If strRet = "" And _
intLeftlength = intStart And _
pAsciiLenB(strChar1) > 1 Then
' 開始位置の全角途中指定時の補正
strRet = " "
Else
strRet = strRet & strChar1
End If
If pAsciiLenB(strRet) > varLength Then
' 文字列長の全角途中指定時の補正
strRet = Left(strRet, Len(strRet) - 1) & " "
End If
If pAsciiLenB(strRet) >= varLength Then
Exit For
End If
End If
Next i
pAsciiMidB = strRet
Else
pAsciiMidB = ""
End If
End Function

' @(f)
'
' 機能 : ASCIIでのRightB
'
' 返り値 : 文字列
'
' 引き数 : ARG1 - 文字列(Unicode)
' ARG2 - (ASCIIでの)文字列長
'
' 機能説明 : 文字列の右端からASCIIコードでのバイト数分の文字列を返す。
'
' 備考 :
'
Public Function pAsciiRightB(ByVal strValue As String, _
ByVal intLength As Integer) As String
Dim intValueLength As Integer

intValueLength = pAsciiLenB(strValue)

If intLength <= 0 Then
pAsciiRightB = ""
ElseIf intValueLength > intLength Then
pAsciiRightB = pAsciiMidB(strValue, intValueLength - intLength + 1)
Else
pAsciiRightB = strValue
End If
End Function
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。