?
Solved

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

Posted on 2003-03-16
6
Medium Priority
?
165 Views
Last Modified: 2010-05-01
     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
0
Comment
Question by:vitalstrike82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:robertlees
ID: 8149345
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.
0
 

Author Comment

by:vitalstrike82
ID: 8149423
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
0
 
LVL 11

Accepted Solution

by:
supunr earned 225 total points
ID: 8149484
Private Function RemoveTags(taggedStr As String) As String
    Dim TagStartAt As Long
    Dim TagEndAt As Long
    Dim TagVal As String
    RemoveTags = taggedStr
    Do
        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
    Loop
End Function


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

Good Luck!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:cocomobasic
ID: 8149508
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>>"
   
    Do
   
        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
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8150523
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
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8150525
P.s.: the line with oreg.execute was only for debugging
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question