vbscript - Replace all spaces

后端 未结 3 1644
自闭症患者
自闭症患者 2020-12-19 21:51

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 (f

相关标签:
3条回答
  • 2020-12-19 22:35

    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> 
    
    0 讨论(0)
  • 2020-12-19 22:42

    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.

    0 讨论(0)
  • 2020-12-19 22:43

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

    Replace(strPostcode," ","",1,-1)
    
    0 讨论(0)
提交回复
热议问题