CodeItBetter Programming Another VB Programming Blog

How to Remove two or more hyphens from a string

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'String Manipulation - How to Remove two or more hyphens from a string
Sub main()
    Dim sText As String
    sText = "This test-string. This is -- the test-string. " & _
            "This is --------- the test-string. This is - the ------ " & _
            "test - string. This is the test--string."
    Debug.Print sText
    Do While InStr(sText, "--") > 0
        sText = Replace(sText, "---", "--")
        sText = Replace(sText, "--", "")
        sText = Replace(sText, "  ", " ")
    Loop
    Debug.Print sText
End Sub
 
'Or

Sub main()
    'You Need to set reference to Microsoft VBScript Regular Expressions 5.5
    Dim sText As String
    Dim re As RegExp, m As MatchCollection
    Set re = New RegExp
 
    sText = "This test-string. This is -- the test-string. " & _
            "This is --------- the test-string. This is - the ------ " & _
            "test - string. This is the test--string."
    Debug.Print sText
 
    re.Global = True
    re.Pattern = "[-]{2,}"
    sText = re.Replace(sText, "")
    re.Pattern = "[ ]{2,}"
    sText = re.Replace(sText, " ")
    Debug.Print sText
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.