2012年8月11日土曜日

VBScript用文字列関連の関数(1)


VBScript用の文字列関連の関数です。
サクラエディタのマクロで使用するために作成しましたが、VBScriptやVBA等で動作します。
'文字列から末尾の改行を除去します。
Function getString(ByRef strLine)
    If endsWith(strLine, vbCrLf) Then
        getString = Left(strLine, Len(strLine) - Len(vbCrLf))
    ElseIf endsWith(strLine, vbCr) Then
        getString = Left(strLine, Len(strLine) - Len(vbCr))
    ElseIf endsWith(strLine, vbLf) Then
        getString = Left(strLine, Len(strLine) - Len(vbLf))
    Else
        getString = strLine
    End If
End Function

'検査対象の文字列が指定の文字列で始まっているか検査します。
Function startsWith(ByRef strStr, ByRef strPrefix)
    startsWith = False
    If Len(strStr) < Len(strPrefix) Then
        Exit Function
    End If
    If InStr(1, strStr, strPrefix) = 1 Then
        startsWith = True
    End If
End Function

'検査対象の文字列が指定の文字列で終わっているか検査します。
Function endsWith(ByRef strStr, ByRef strSuffix)
    endsWith = False
    If Len(strStr) < Len(strSuffix) Then
        Exit Function
    End If
    If Right(strStr, Len(strSuffix)) = strSuffix Then
        endsWith = True
    End If
End Function

0 件のコメント:

コメントを投稿