saurav
saurav

Reputation: 422

What does these lines of code do in VBScript?

I am converting them to Jython script and I felt all it does is remove spaces at ends

function test (strField)
  Dim re 
  Set re = New RegExp
  re.Pattern = "^\s*"
  re.MultiLine = False
  strField = re.replace(strField,"")
End Function

Upvotes: 0

Views: 89

Answers (1)

user692942
user692942

Reputation: 16681

It uses the RegExp object in VBScript to check for whitespace \s at the start of the variable passed into the Sub / Function called strField. Once it identifies the whitespace it uses the Replace() method to remove any matched characters from the start of the string.

As @ansgar-wiechers has mentioned in the comments it is just an all whitespace implementation of the LTrim() function.

I'm assuming this is meant to be a Function though (haven't tested but maybe VBScript accepts Fun as shorthand for Function, not something I'm familiar with personally) with that in mind it should return the modified strField value as the result of the function. Would also recommend using ByVal to stop the strField value after it is manipulated bleeding out of the function.

Function test(ByVal strField)
  Dim re 
  Set re = New RegExp
  re.Pattern = "^\s*"
  re.MultiLine = False
  strField = re.replace(strField,"")
  test = strField
End Function

Usage in code:

Dim testin: testin = "             some whitespace here"
Dim testout: testout = test(testin)
WScript.Echo """" & testin & """"
WScript.Echo """" & testout & """"

Output:

"             some whitespace here"
"some whitespace here"

Upvotes: 3

Related Questions