vbScript - Some Useful String Functions

Reverend Jim 1 Tallied Votes 1K Views Share

vbScript - Some Useful String Functions

Please see my post vbScript - The Basics for more details on vbScript.

vbScript provides a number of functions for manipulating strings. I find that a few more simple functions would have made things a lot simpler. For example, I find myself checking to see if a string starts with a given string. What I have to code is

If len(str1) < len(str2) Then
    StartsWith = False
Else If Left(str1,len(str2)) = str2 Then
    StartsWith = True
Else
    StartsWIth = False
End If

It would be much clearer if I could do

If StartsWith(str1,str2) Then

Similarly, I will frequently drop characters from the beginning or end of a string. This can result in some convoluted calculations inside calls to Left, Right, and Mid. Whenever I need a string function I code it up once and add it to my StrFuncs.vbs include file. They are detailed here. See the comments in the code snippet for details.

''#region Header                                                                        '
''                                                                                      '
''  Name:                                                                               '
''                                                                                      '
''      StrFuncs.vbs                                                                    '
''                                                                                      '
''  Description:                                                                        '
''                                                                                      '
''      A collection of useful string functions.                                        '
''                                                                                      '
''      Lpad (val, width, padch)                                                        '
''                                                                                      '
''          Return <val> padded on the left with <padch> to the given width.            '
''                                                                                      '
''      Rpad (val, width, padch)                                                        '
''                                                                                      '
''          Return <val> padded on the right with <padch> to the given width.           '
''                                                                                      '
''      Repeat (str, ntimes)                                                            '
''                                                                                      '
''          Return <ntimes> repetitions of <str>                                        '
''                                                                                      '
''      StartsWith (line, str)                                                          '
''                                                                                      '
''          Returns True if <line> starts with <str>. Handles the case where <str> Is   '
''          shorter than <line>.                                                        '
''                                                                                      '
''      EndsWith (line, str)                                                            '
''                                                                                      '
''          Same as StartsWith but checks the other end for a match.                    '
''                                                                                      '
''      Drop (str, nchars)                                                              '
''                                                                                      '
''          Drop nchars from the beginning of the string. If nchars < 0 then Drop       '
''          -nchars from the end of the string.                                         '
''                                                                                      '
''      Capitalize (str)                                                                '
''                                                                                      '
''          Capitalizes the first letter of <str> and every letter following a blank    '
''                                                                                      '
''      TitleCaps (str)                                                                 '
''                                                                                      '
''          Similar to Capitalize except certain embedded words are not capitalized.    '
''          As is more or less standard in titles, "a", "an", "the", etc.               '
''                                                                                      '
''  Notes:                                                                              '
''                                                                                      '
''      Lpad will truncate on the left if <width> is less than the resulting string.    '
''      Rpad will truncate on the right. Similarly, if you Drop more characters than    '
''      are available in a string, a null string is returned.                           '
''                                                                                      '
''  Audit:                                                                              '
''                                                                                      '
''      2017-06-02  rj  original code                                                   '
''                                                                                      '
''#endregion                                                                            '

''Return <val> as a string padded on the left with <padch> to a max width of <width>    '
Function Lpad (val, width, padch)
    Lpad = Right(Repeat(padch,width-len(cStr(val))) & val,width)
End Function

''Return <val> as a string padded on the right with <padch> to a max width of <width>   '
Function Rpad (val, width, padch)
    Rpad = Left(val & Repeat(padch,width-len(cStr(val))),width)
End Function

''Return <ntimes> repetitions of <str>                                                  '
Function Repeat (str, ntimes)
    Repeat = ""
    Dim i: For i = 1 To ntimes
        Repeat = Repeat & str
    Next
End Function

''Return True if <line> starts with <str>                                               '
Function StartsWith (line, str)

    Select Case True
        Case len(line) < len(str)
            StartsWith = False
        Case Left(line,len(str)) = str
            StartsWith = True
        Case Else
            StartsWIth = False
    End Select

End Function

''Return True if <line> ends with <str>                                                 '
Function EndsWith (line, str)

    Select Case True
        Case len(line) < len(str)
            EndsWith = False
        Case Right(line,len(str)) = str
            EndsWith = True
        Case Else
            EndsWIth = False
    End Select

End Function

''Drop nchars from the beginning (ncar > 0) or end (nchar < 0) of a string              '
Function Drop (str, nchars)

    Select Case True
        Case nchars > 0
            Drop = Mid(str,1+nchars)
        Case nchars < 0
            If Len(str)+nchars < 0 Then Drop = "": Else: Drop = Mid(str,1,Len(str)+nchars)
        Case nchars = 0
            Drop = str
    End Select

End Function

''Capitalize the first letter and every letter immediately following a blank            '
Function Capitalize (str)

    Capitalize = Ucase(Mid(str,1,1))

    Dim i
    Dim prev

    prev = Capitalize

    For i = 2 To Len(str)
        curr = Mid(str,i,1)
        If prev = " " Then
            Capitalize = Capitalize & Ucase(curr)
        Else
            Capitalize = Capitalize & Lcase(curr)
        End If
        prev = curr
    Next

End Function

''Capitalize the first letter of every word except for those in a list                  '
Function TitleCaps (ByVal str)

    Dim rep
    Dim key

    str = Replace(str,"."," ")
    str = Replace(str,"_"," ")
    str = Capitalize(str)

    Set rep = CreateObject("Scripting.Dictionary")
        rep.Add "."    ," "
        rep.Add "_"    ," "
        rep.Add "("    ,"["
        rep.Add ")"    ,"]"
        rep.Add " On " ," on "
        rep.Add " Is " ," is "
        rep.Add " By " ," by "
        rep.Add " To " ," to "
        rep.Add " The "," the "
        rep.Add " A "  ," a "
        rep.Add " An " ," an "
        rep.Add " And "," and "

    For Each key In rep.Keys
        str = Replace(str,key,rep(key))
    Next

    TitleCaps = str

End Function

''Test code                                                                             '
If StrComp(Wscript.ScriptName,"strfuncs.vbs",vbTextCompare) = 0 Then
    Wscript.Echo Lpad(5,5,"0")
    Wscript.Echo Rpad(5,5,"0")
    Wscript.Echo cStr(StartsWith("abcde","ab"))
    Wscript.Echo cStr(EndsWith("abcde","de"))
    Wscript.Echo Capitalize("THIS IS A STRING")
    Wscript.Echo TitleCaps("THIS IS A STRING")
    WScript.Echo Drop("abcdefghij",5)
    WScript.Echo Drop("abcdefghij",-5)
End If
SN Technologies -4 Service Provider

Nice information. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.