List of characters in VBScript that require escape characters - vbscript

I just learned that $ requires an escape character. What other special characters are there in VBScript?
Also is there a boolean function where I can find out if a character is a special character?

Huh? WScript.Echo "$" outputs $ without any escaping. The only special character in a VBScript string literal is the double quote (use two in a row for a literal double quote within a string).

You have to escape bigmoney when using it in VBScript regular expressions, but that is a very specific case. You cannot use it as you are used to in some BASIC flavours, VBA or VB to assign the String primitive to a variable.
(like
10 FOR I = 1024 TO 1063
20 A$ = A$ + CHR$(PEEK(I))
30 NEXT I
40 ? A$;
50 A$ = ""
60 GOTO 10
for the C64 or
Dim i, original$, final$
original$ = "Hello World!"
' Premature optimization rules! xxx$ functions are faster than xxx functions!
final$ = Left$(original$, 3) & Chr$(112) & Chr$(32) & Chr$(109) & Mid$(original$, 2, 1) & Right$(original$, 7)
MsgBox final$
In good ol' VB6)
Just eliminate the $ in the latter example, you don't need them.
If you really, really, really want to use the $ in routine or variable naming, you can always use the brackets like:
Sub [Wow! does thi$ really works? I'm a 1337 h4x0rz!]
MsgBox "Yes it does!"
End Sub
[Wow! does thi$ really works? I'm a 1337 h4x0rz!]
Edit;
Extra-Free-Bonus: A specialcharacter recognition function:
Public Function isSpecialCharacter(byVal myChar)
isSpecialCharacter = (myChar="""")
End Function

Related

Calculator with function Add concatenates result instead of addition [duplicate]

On every site that talks about VBScript, the '&' operator is listed as the string concatenation operator. However, in some code that I have recently inherited, I see the '+' operator being used and I am not seeing any errors as a result of this. Is this an accepted alternative?
The & operator does string concatenation, that is, forces operands to be converted to strings (like calling CStr on them first). +, in its turn, forces addition if one of the expressions is numeric. For example:
1 & 2
gives you 12, whereas
1 + 2
"1" + 2
1 + "2"
give you 3.
So, it is recommended to use & for string concatenation since it eliminates ambiguity.
The + operator is overloaded, whereas the & operator is not. The & operator only does string concatenation. In some circles the & operator is used as a best practice because it is unambiguous, and therefore cannot have any unintended effects as a result of the overloading.
+ operator might backfire when strings can be interpreted as numbers. If you don't want nasty surprises use & to concatenate strings.
In some cases the + will throw an exception; for example the following:
Sub SimpleObject_FloatPropertyChanging(fvalue, cancel)
'fvalue is a floating point number
MsgBox "Received Event: " + fvalue
End Sub
You will get an exception when the COM object source fires the event - you must do either of the following:
MsgBox "Received Event: " & fvalue
or
MsgBox "Received Event: " + CStr(fvalue)
It may be best in either case to use CStr(value); but using & per above comments for string concatenation is almost always best practice.

The system cannot find the file if its path/name contains a space

Path = split(wscript.scriptFullName, wscript.scriptname)(0)
CreateObject("wscript.shell").run(Path & "Name.txt")
The above script works fine if both the file path and file name contain no spaces.
If either contains a space, the result will be;
Error: The system cannot find the file specified.
How can I fix the error?
The rules are fairly simple:
All strings have to start and end with double quotes to be a valid string.
Dim a
a = "Hello World" 'Valid string.
a = "Hello World 'Not valid and will produce an error.
Any use of variables must use the String Concatenation character & to combine them with strings.
Dim a: a = "Hello"
Dim b
b = a & " World" 'Valid concatenated string.
b = a " World" 'Not valid and will produce an error.
As double quotes are used to define a string, all instances of double quotes inside a string must be escaped by doubling the quotes "" but Rule 1. still applies.
Dim a: a = "Hello"
Dim b
b = """" & a & " World""" 'Valid escaped string.
b = """ & a & " World""" 'Not valid, start of string is not complete
'after escaping the double quote
'producing an error.
Follow these three rules and you won't go far wrong.
With those in mind the above line would need to be;
CreateObject("wscript.shell").run("""" & Path & "Name.txt""")
to generate a string surrounded by literal double quotes.
Useful Links
VBS with Space in File Path
Adding quotes to a string in VBScript
Breaking a String Across Multiple Lines (More on string concatenation).
CreateObject("wscript.shell").run(""""Path & "Name.txt""")
is how.

vbscript - Replace all spaces

I have 6400+ records which I am looping through. For each of these: I check that the address is valid by testing it against something similar to what the Post Office uses (find address). I need to double check that the postcode I have pulled back matches.
The only problem is that the postcode may have been inputted in a number of different formats for example:
OP6 6YH
OP66YH
OP6 6YH.
If Replace(strPostcode," ","") = Replace(xmlAddress.selectSingleNode("//postcode").text," ","") Then
I want to remove all spaces from the string. If I do the Replace above, it removes the space for the first example but leave one for the third.
I know that I can remove these using a loop statement, but believe this will make the script run really slow as it will have to loop through 6400+ records to remove the spaces.
Is there another way?
I didn't realise you had to add -1 to remove all spaces
Replace(strPostcode," ","",1,-1)
Personally I've just done a loop like this:
Dim sLast
Do
sLast = strPostcode
strPostcode = Replace(strPostcode, " ", "")
If sLast = strPostcode Then Exit Do
Loop
However you may want to use a regular expression replace instead:
Dim re : Set re = New RegExp
re.Global = True
re.Pattern = " +" ' Match one or more spaces
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("O P 6 6 Y H.", "")
Set re = Nothing
The output of the latter is:
D:\Development>cscript replace.vbs
OP66YH.
OP66YH.
OP66YH.
D:\Development>
This is the syntax Replace(expression, find, replacewith[, start[, count[, compare]]])
it will default to -1 for count and 1 for start. May be some dll is corrupt changing the defaults of Replace function.
String.Join("", YourString.Split({" "}, StringSplitOptions.RemoveEmptyEntries))
Because you get all strings without spaces and you join them with separator "".

VB6 getting ride of large spaces

Hey all, i am trying to replace large spaces between text with just one. My output looks like this right now:
5964215">
This is just the first example of the spaces
5964478">
This would be the 2nd example of showing how many spaces this thing has in each sentence.
5964494">
That comes from a textbox that has multi-line to true. Here is what it looks like when it doesn't have multi-line to true.
http://www.june3rdsoftware.com/forums/vb6.jpg
I can not seem to get the spaces to go away! BTW, this text is from a webpage if that makes any difference.
David
According to the suggestion of MvanGeest, here is some VB code to replace blocks of white spaces:
Sub test()
Dim x As String, y As String
x = "abcd defg 1233"
Dim re As New RegExp
re.Pattern = "\s+"
re.Global = True
y = re.Replace(x, " ")
Debug.Print y
End Sub
To make this work, you will have to add a reference to "Microsoft VBScript Regular Expresssions" to your project.
Assuming no regex support, why not set up a simple state machine that will set the state=1 when a space is found and set state=0 once a non-space is encountered. You can move char by char when state=0 (thus copying over only 1 space per series of spaces).
Also assuming no regex, you could try something like
str = "long text with spaces "
i = LenB(str)
str = Replace(str, " ", " ")
Do While LenB(str) <> i
i = LenB(str)
str = Replace(str, " ", " ")
Loop
Of course this code could be optimized for long space sequences but it might be all you need as well

How to use substring in vbscript within a xsl page

I am trying to replace the double quotes in a string with a single quote, got the following code but get error message saying "Object Required strLocation"
Sub UpdateAdvancedDecisions(strLocation)
Dim d
Dim strLLength
strLLength = Len(strLocation) - 1
For d = 0 To strLLength
alert strLocation
strValue = strLocation.Substring(2,3)
If strLocation.substring(d,d+1)=" " " Then
strLLength = strLLength.substring(0, d) + "'" + strLLength.substring(d + 1,strLLength.length)
Next
End Sub
As Helen said, you want to use Replace, but her example assigned the result to your weird strLLength variable. Try this instead:
strLocation = Replace(strLocation, """", "'")
This one line does the job you asked about and avoids all the code currently in your given subroutine.
Other things that are problems in the code you posted:
a variable holding a number like the length of a string would not have a "str" prefix, so strLLength is misleading
strings in VBScript are indexed from 1 through length, not 0 through length-1
there is no "alert" keyword in VBScript
you assign a value to strValue, then never use it again
you need to use Mid to get a substring, there is no "substring" string method in VBScript
c = Mid(strLocation, d, 1) ' gets one character at position d
The more I look at this, the more clear it is that its some JavaScript that you're trying to run as VBScript but are not translating at all correctly.
Use a reference for VBScript like one of the following:
MSDN Library: VBScript Language Reference
W3Schools VBScript Tutorial

Resources