Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 384
ClareBear Avatar asked Dec 06 '11 08:12

ClareBear


People also ask

How to remove Space in VBScript?

The Trim function removes spaces on both sides of a string. Tip: Also look at the LTrim and the RTrim functions.

How will you trim the spaces on the right of a string using VBScript?

RTrim is used to trim/remove the spaces from the right-hand side of the specified String.

What is replace in VBScript?

The Replace function replaces a specified part of a string with another string a specified number of times.

How do you create a function in VBScript?

Function Definition The most common way to define a function in VBScript is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with an End Function keyword, which indicates the end of the function.


3 Answers

I didn't realise you had to add -1 to remove all spaces

Replace(strPostcode," ","",1,-1)
like image 180
ClareBear Avatar answered Oct 14 '22 03:10

ClareBear


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> 
like image 28
Richard Avatar answered Oct 14 '22 05:10

Richard


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.

like image 33
Buddha Avatar answered Oct 14 '22 03:10

Buddha