Solved

Parsing a String

Posted on 2004-03-20
6
367 Views
Last Modified: 2010-05-01
What is the best way to parse out a substring within the following string?

Daily Operations Status ReportDaily Operations Status ReportCritical Application Cycle End TimesInsurance/ISAInsurance DataBase(Critical Path - BT30004) ended at: 2:00Insurance DataBase(LASTISA) ended at: 3:00

I wrote the following parse function to pull out the time, but it returns: ) ended at: 2:00

Private Function FilterBT4(BT4Str)
  Dim i As Integer, b As Integer
  Dim FinalStr As String
  i = InStr(1, BT4Str, "4) ended at: ")
  If i Then
   i = i + 1
   b = InStr(i, BT4Str, "Insurance")
    If b Then
     FinalStr = Mid(BT4Str, i, b - i)
     FilterBT4 = FinalStr
    End If
 End If
End Function

I just want it to pull out:   2:00

Any ideas??
Thanks
Rich
0
Comment
Question by:rkckjk
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
Comment Utility
Dim pStart as Integer, pEnd as Integer
Dim pTime as String

pStart = InStr(1, Text1.Text, "ended at:") + 10
pEnd = InStr(pStart, Text1.Text, "Insurance")
pTime = Trim(Mid(Text1.Text, pStart, pEnd - pStart))

that is assuming the text u want to parse out of is Text1, u can change that
0
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
Comment Utility
pTime would then be ur time.. u can test it by putting:
MsgBox pTime
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
'Assuming BT4Str = "Daily Operations Status ReportDaily Operations Status ReportCritical Application Cycle End TimesInsurance/ISAInsurance DataBase(Critical Path - BT30004) ended at: 2:00Insurance DataBase(LASTISA) ended at: 3:00"

Then this is your function:

Private Function FilterBT4(BT4Str)
FilterBT4 = Right(BT4Str, Len(BT4Str) - InStrRev(BT4Str, "ended at:", -1) - 9)
End Function

S
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
Shauli, your code will find the "ended at: 3:00" which is at the VERY END of the string, and not the ended at: 2:00 which the original questioner was searching for.

rkckjk, your original code:

Private Function FilterBT4(BT4Str)
  Dim i As Integer, b As Integer
  Dim FinalStr As String
  i = InStr(1, BT4Str, "4) ended at: ")
  If i Then
   i = i + 1
   b = InStr(i, BT4Str, "Insurance")
    If b Then
     FinalStr = Mid(BT4Str, i, b - i)
     FilterBT4 = FinalStr
    End If
 End If
End Function


was VERY close, you simply needed to add 11 to i, rather than 1:

Private Function FilterBT4(BT4Str)
  Dim i As Integer, b As Integer
  Dim FinalStr As String
  i = InStr(1, BT4Str, "4) ended at: ")
  If i Then
   i = i + 11
   b = InStr(i, BT4Str, "Insurance")
    If b Then
     FinalStr = Mid(BT4Str, i, b - i)
     FilterBT4 = FinalStr
    End If
 End If
End Function


The extra 10 is the number of characters in 'ended at: ', since the value of i (in the line -- i = InStr(1, BT4Str, "4) ended at: ")) is the location of the first 'e' in 'ended;, and you want the location of the 2 that foloows thast location...hence you must add the numbers of characters to offset to the right, to get the value you want.

AW
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
Arthur_Wood, You are absolutely right, I misread the question :(
S
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 50 total points
Comment Utility
hey, it happens to the best of us.  Just wanted to make sure that rkckjk did not get confused.

AW
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now