VBA字符串处理

'概要:     SHIFT_JIS「全角文字判定(ASCII:0~255の範囲外)」
'引数:     strHantei:判定キャラクター
'戻り値:   True:全角文字, False:全角文字以外
Function isZenkaku(strHantei As String) As Boolean
    Dim intAscii As Integer
    intAscii = Asc(strHantei)
    isZenkaku = False
    'Ascii255の範囲外
    If intAscii > 255 Or intAscii < 0 Then
        isZenkaku = True
    End If
End Function

'概要:     SHIFT_JIS「半角カナ判定(ASCII:A1~DF)」
'引数:     strHantei:判定キャラクター
'戻り値:   True:半角カナ, False:半角カナ以外
Function isHankakukana(strHantei As String) As Boolean
    Dim intAscii As Integer
    intAscii = Asc(strHantei)
    isHankakukana = False
    'ASCII:A1~DF
    If intAscii >= 161 And intAscii <= 223 Then
        isHankakukana = True
    End If
End Function

'概要:     SHIFT_JIS「英数字判定(ASCII:「00~A0」「E0~FF」)」
'引数:     strHantei:判定キャラクター
'戻り値:   True:英数字記号, False:英数字記号以外
Function isEisuji(strHantei As String) As Boolean
    Dim intAscii As Integer
    intAscii = Asc(strHantei)
    isEisuji = False
    'ASCII:「00~A0」「E0~FF」
    If (intAscii >= 0 And intAscii <= 160) Or (intAscii >= 224 And intAscii <= 255) Then
        isEisuji = True
    End If
End Function

'概要:     キャラクター類型を判定
'引数:     strHantei:判定キャラクター
'戻り値:   0:英数字記号, 1:半角カナ, 2:全角文字
Function hanteiCharType(strHantei As String) As String
    '英数字判定(ASCII:「00~A0」「E0~FF」)
    If isEisuji(strHantei) Then
        hanteiCharType = "0"
    '半角カナ判定(ASCII:A1~DF)
    ElseIf isHankakukana(strHantei) Then
        hanteiCharType = "1"
    '全角文字判定(ASCII:255の範囲外)
    ElseIf isZenkaku(strHantei) Then
        hanteiCharType = "2"
    End If
End Function

'概要:     複数なスペース(半角)を一つに変わる
'引数:     str:対象ストリング
'戻り値:   複数なスペース(半角)を一つになったストリング
Function replaceSpacesToOne(str As String) As String
    Dim strReturn As String
    Dim Matches As Object
    Dim Match As Object
    Dim myReg As Object

    Set myReg = CreateObject("VBSCRIPT.REGEXP")
    With myReg
        .Pattern = "\s{2,}" '二つ以上スペース
        .Global = True
        replaceSpacesToOne = .Replace(str, " ")
    End With
End Function

猜你喜欢

转载自wiseboyloves.iteye.com/blog/2393989