Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Data > Strings

Strings

  1. Denis St-Pierre's Val( ) function
  2. Pad strings

 

Val( )
VBScript Code:
Function Val( myString )
' Val Function for VBScript (aka ParseInt Function in VBScript).
' By Denis St-Pierre.
' Natively VBScript has no function to extract numbers from a string.
' Based shamelessly on MS' Helpfile example on RegExp object.
' CAVEAT: Returns only the *last* match found
'         (or, with objRE.Global = False, only the *first* match)

    Dim colMatches, objMatch, objRE, strPattern

    ' Default if no numbers are found
    Val = 0

    strPattern = "[-+0-9]+"       ' Numbers positive and negative; use
                                  ' "ˆ[-+0-9]+" to emulate Rexx' Value()
                                  ' function, which returns 0 unless the
                                  ' string starts with a number or sign.
    Set objRE = New RegExp        ' Create regular expression object.
    objRE.Pattern    = strPattern ' Set pattern.
    objRE.IgnoreCase = True       ' Set case insensitivity.
    objRE.Global     = True       ' Set global applicability:
                                  '   True  => return last match only,
                                  '   False => return first match only.
    Set colMatches = objRE.Execute( myString )  ' Execute search.
    For Each objMatch In colMatches             ' Iterate Matches collection.
        Val = objMatch.Value
    Next
    Set objRE= Nothing
End Function
 
Requirements:
Windows version: Windows 2000 or later, or Internet Explorer 5.0 or later
Network: any
Client software: Internet Explorer 5.0 or later for pre-Windows 2000
Script Engine: any
Summarized: Works in Windows 2000 or later, or Internet Explorer 5.0 or later.
 
[Back to the top of this page]
 
Sample Script
VBScript Code:
Option Explicit

WScript.Echo Val( "That 18 year old is darned hot." )
WScript.Echo Val( "That 18 year old is darned hot. She was born in 1980" )
 
Sample output:
18
1980
 
[Back to the top of this page]

 

Pad strings

Sometimes I need to display results in columns. Using tabs may help but isn't exactly "fail-safe".

For that reason I wrote the VBScript functions LeftPad and RightPad.

Some examples to explain their usage:

myString = "ABCDEF"
WScript.Echo Left( myString, 3 )
WScript.Echo Left( myString, 9 )

will display:

ABC
ABCDEF
myString = "ABCDEF"
WScript.Echo LeftPad( myString, 3, "+" )
WScript.Echo LeftPad( myString, 9, "+" )

will display:

ABC
ABCDEF+++

LeftPad will often be used to pad strings with spaces.

RightPad is LeftPad's counterpart:

myString = "ABCDEF"
WScript.Echo Right( myString, 3 )
WScript.Echo Right( myString, 9 )

will display:

DEF
ABCDEF
myString = "ABCDEF"
WScript.Echo RightPad( myString, 3, "+" )
WScript.Echo RightPad( myString, 9, "+" )

will display:

ABC
+++ABCDEF

RightPad will often be used to pad numbers (in strings) with zeroes.

Look at this combination:

Dim arrName(1)
Dim arrAge(1)
arrName(0) = "John Doe"
arrName(1) = "Vanessa James"
arrAge(0)  = 9
arrAge(1)  = 65
WScript.Echo LeftPad( "NAME:", 20, " " ) & RightPad( "AGE:", 4, " " )
For i = 0 To UBound( arrName )
	WScript.Echo LeftPad( arrName(i), 20, " " ) & RightPad( arrAge(i), 4, " " )
Next

will display:

NAME:               AGE:
John Doe               9
Vanessa James         65

LeftPad, optionally combined with RightPad, is perfect for displaying lists, e.g. WMI class properties and their values.

 

VBScript code:

Function LeftPad( strText, intLen, chrPad )
	'LeftPad( "1234", 7, "x" ) = "1234xxx"
	'LeftPad( "1234", 3, "x" ) = "123"
	LeftPad = Left( strText & String( intLen, chrPad ), intLen )
End Function
Function RightPad( strText, intLen, chrPad )
	'RightPad( "1234", 7, "x" ) = "xxx1234"
	'RightPad( "1234", 3, "x" ) = "234"
	RightPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

 

[Back to the top of this page]


page last modified: 2016-09-19; loaded in 0.0054 seconds