• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

How to scan the entire string to look for a specific character

     i declare a variable: dim str as string. Next i extract a data from the database and store it into the str variable.
      Now i need to scan the string. The data extract from my database contains the characters which is shown here: eg: Name <<Tag>> Age <<Tag>>.
      Now i stuck as i do not know how to scan the string which is store in the str variable. i want to search for the  "<<Tag>>" character and delete the "<<Tag>>" character so my result of the scanning will end in this way: "Name      Age"
      can anyone pls help me with this problem...
Thank you very much
1 Solution
In VB6 you can use the Replace statement...

strNewString = Replace(str, "<<Tag>>", "")

This will replace every occurrence of <<Tag>> found in str with an empty string.
vitalstrike82Author Commented:
what if the string contains the characters like eg:    name <<Tag1>> Age <<Tag2>>. In this way i cant use the replace statement. so how do i write codes to scan....
Thanks for helping me
Private Function RemoveTags(taggedStr As String) As String
    Dim TagStartAt As Long
    Dim TagEndAt As Long
    Dim TagVal As String
    RemoveTags = taggedStr
        TagStartAt = InStr(1, RemoveTags, "<<Tag")
        ' no tags found
        If (TagStartAt = 0) Then Exit Do
        TagEndAt = InStr(TagStartAt, RemoveTags, ">>")
        TagVal = Mid(RemoveTags, TagStartAt, TagEndAt - TagStartAt + 2)
        RemoveTags = Replace(RemoveTags, TagVal, "")  ' replace found tag with empty string
End Function

' somewhere in your code....
msgbox RemoveTags(rsRecordset.Fields("FieldName").Value)

Good Luck!
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Option Explicit

Private Sub Command1_Click()
Dim str As String
Dim strNew As String
Dim strTemp As String
Dim i As Integer

Dim StartingChar As Integer
Dim EndingChar As Integer

    str = "name <<Tag1>> Age <<Tag2>>"
        StartingChar = InStr(1, str, "<<")
        EndingChar = InStr(1, str, ">>")
        strTemp = Mid(str, StartingChar, Abs(Len(str) - EndingChar - StartingChar))
        str = Replace(str, strTemp, "")
        StartingChar = InStr(1, str, "<<")
        EndingChar = InStr(1, str, ">>")
    Loop While StartingChar <> 0 And EndingChar <> 0
    MsgBox str

End Sub
This code will remove any tag regardless of name/type

Dim oRegExp As New RegExp
Dim cLine As String
Dim oMatches As MatchCollection
Dim oMatch As Match
With oRegExp
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
End With
Set oStrmInput = oFso.OpenTextFile("c:\temp\test.txt")
cLine = "name <<Tag1>> Age <<Tag2>>."
oRegExp.Pattern = "(<<(.*?)>>)"
Set oMatches = oRegExp.Execute(cLine)
cLine = oRegExp.Replace(cLine, "")
Debug.Print cLine
P.s.: the line with oreg.execute was only for debugging

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now