2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBScriptでカタカナの全角・半角変換を行う関数

Last updated at Posted at 2020-08-01

下記の記事のロジックの応用で、カタカナの全角・半角変換を行う関数も作成しました。
VBScriptで全角・半角変換を行う関数

#ソースコード

Option Explicit

Const ZENKANA = "、。「」ー・゜゛アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォャュョッ"
Const HANKANA = "、。「」ー・゚゙アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォャュョッ"
Const ZENKANA2 = "ヴガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポ"
Const HANKANA2 = "@ヴガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポ" '1文字目はダミー

'*****************************************************************************
'[概要] 半角カタカナを全角に変換する
'[引数] 変換対象文字列
'[戻値] 変換後文字列
'*****************************************************************************
Function StrConvWideKana(ByVal strWord)
    Dim i, j, strChar, strChar2
    For i = 1 To Len(strWord)
        strChar = Mid(strWord, i, 1)
        
        strChar2 = Mid(strWord & "@", i, 2) '最終文字の後ろに1文字ダミー文字を追加しておく
        j = InStr(2, HANKANA2, strChar2) '2文字目から検索
        '偶数の時
        If j > 0 And j Mod 2 = 0 Then
            '例:ガ→ガ
            strChar = Mid(ZENKANA2, j / 2, 1)
            i = i + 1 '濁点分1文字進める
        Else
            j = InStr(1, HANKANA, strChar)
            If j > 0 Then
                strChar = Mid(ZENKANA, j, 1)
            End If
        End If
        StrConvWideKana = StrConvWideKana & strChar
    Next
End Function

'*****************************************************************************
'[概要] 全角カタカナを半角に変換する
'[引数] 変換対象文字列
'[戻値] 変換後文字列
'*****************************************************************************
Function StrConvNarrowKana(ByVal strWord)
    Dim i, j, strChar
    For i = 1 To Len(strWord)
        strChar = Mid(strWord, i, 1)
        j = InStr(1, ZENKANA2, strChar)
        If j > 0 Then
            '例:ガ→ガ
            strChar = Mid(HANKANA2, j * 2, 2)
        Else
            j = InStr(1, ZENKANA, strChar)
            If j > 0 Then
                strChar = Mid(HANKANA, j, 1)
            End If
        End If
        StrConvNarrowKana = StrConvNarrowKana & strChar
    Next
End Function

#関連記事
VBScriptで全角・半角変換を行う関数
VBScriptでカタカナの全角・半角変換を行う関数 ※この記事
VBScriptでひらがな・カタカナ変換を行う関数

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?