Valleriani
asked on
VB6 - A quick/correct way to get specific text from a string?
Hello, I am with this string, as an example:
<Ttest> This is a test, so you should know this is just a test area. We test thing here. This can be a very long description, and other things like that. Mike, John, Luke, Paul, Buzz, and Test are here with you. There is a staff on the floor.
What I want to do is grab the line of 'users' in the current room. So I'd like to extract "Mike, John, Luke, Paul, Buzz, and Test are here with you." from the line above.
Issue is, I'm not sure how to exactly do it. What I was thinking is checking are here with you. first, then going back via " ," (space then ,) until there is none left or " ," occurs in more then one word from the last " ,".. But this seems wierd/difficult to do.
I know theres not a 100% way to do this, but theres probably a good way that I am not sure about.. Thanks!
<Ttest> This is a test, so you should know this is just a test area. We test thing here. This can be a very long description, and other things like that. Mike, John, Luke, Paul, Buzz, and Test are here with you. There is a staff on the floor.
What I want to do is grab the line of 'users' in the current room. So I'd like to extract "Mike, John, Luke, Paul, Buzz, and Test are here with you." from the line above.
Issue is, I'm not sure how to exactly do it. What I was thinking is checking are here with you. first, then going back via " ," (space then ,) until there is none left or " ," occurs in more then one word from the last " ,".. But this seems wierd/difficult to do.
I know theres not a 100% way to do this, but theres probably a good way that I am not sure about.. Thanks!
Oh, >>is this list of companions always last?
Apart from ...
>>There is a staff on the floor.
Apart from ...
>>There is a staff on the floor.
Dim s As String
Dim marker As Integer
Dim startPos As Integer
s = "This is a test, so you should know this is just a test area. We test thing here. This can be a very long description, and other things like that. Mike, John, Luke, Paul, Buzz, and Test are here with you. There is a staff on the floor."
marker = InStr(s, "are here with you.")
s = Trim(Mid(s, 1, marker + 17))
startPos = InStrRev(s, ". ")
s = Trim(Mid(s, startPos + 1))
MsgBox s
Dim marker As Integer
Dim startPos As Integer
s = "This is a test, so you should know this is just a test area. We test thing here. This can be a very long description, and other things like that. Mike, John, Luke, Paul, Buzz, and Test are here with you. There is a staff on the floor."
marker = InStr(s, "are here with you.")
s = Trim(Mid(s, 1, marker + 17))
startPos = InStrRev(s, ". ")
s = Trim(Mid(s, startPos + 1))
MsgBox s
ASKER
Yes it is always last for the items.
Basicly goes room description, characters, items on floor.
I'll check out what the above poster just posted.. Gonna try it now.
Basicly goes room description, characters, items on floor.
I'll check out what the above poster just posted.. Gonna try it now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hrm confused about the above, didn't do much I think.. I got the whole line except for the item thats on the floor. Sometimes you have to remember the item will not be there, thus that line is already removed. But otherwise in another test I've seem to get the room descriptoin plus the names.
ASKER
Oh cool thanks, will try that one!
ASKER
Your example does work, however what if the ending is like This is a test, so you should know this is just a test area. We test thing here! This can be a very long description, and other things like that! Mike, John, Luke, Paul, Buzz, and Test are here with you.
Not a period, but an "!" is there any way to split on like.. !?. etc?
Not a period, but an "!" is there any way to split on like.. !?. etc?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nevermind, think I got a simple fix, replace all ?! etc with a period before splitting.. Seem good?
And yeah the array seems to be a better solution because I am able to grab them one at a time too!
And yeah the array seems to be a better solution because I am able to grab them one at a time too!
Yes, replace !? with a period before
' Split into sentences.
'
sa1 = Split(s, ".")
' Split into sentences.
'
sa1 = Split(s, ".")
Hi There
Here is an example of how to manipulate strings to get what you want. Of course, if teh complete string is always going to be different then its near impossible, unless you always have at least 2 points of references that will alwatys be the same
In your example, the first point of reference is the "are here with you" and the second point of reference i picked was a period . just before the names. If these 2 always will be the case then my sample code should work just fine for you
To play with it, copy to notepad and save as a .vbs file. then from a CMD prompt, type CScript FileName.vbs which will ouput the result to the window for you. Once you are happy with it, transfer it to your VB6 code
Hope this helps
Regards
Krystian
Here is an example of how to manipulate strings to get what you want. Of course, if teh complete string is always going to be different then its near impossible, unless you always have at least 2 points of references that will alwatys be the same
In your example, the first point of reference is the "are here with you" and the second point of reference i picked was a period . just before the names. If these 2 always will be the case then my sample code should work just fine for you
To play with it, copy to notepad and save as a .vbs file. then from a CMD prompt, type CScript FileName.vbs which will ouput the result to the window for you. Once you are happy with it, transfer it to your VB6 code
Hope this helps
Regards
Krystian
Dim strOutput
Dim iStartPos
Dim iEndPos
Dim iLength
strOutput = strOutput & "This is a test, so you should know this is just a test area. "
strOutput = strOutput & "We test thing here. This can be a very long description, "
strOutput = strOutput & "and other things like that. Mike, John, Luke, Paul, Buzz, and Test "
strOutput = strOutput & "are here with you. There is a staff on the floor."
iEndPos = InStr(strOutput, "are here with you") ' Finds the start point of the string "are here with you"
iStartPos = InStrRev(strOutput, ".", iEndPos) ' Finds the first period before "are here with you"
iLength = (CInt(iEndPos) - CInt(iStartPos)) ' minus the 2 to give us the string between
ShowProgress Mid(strOutput, iStartPos + 2, iLength - 3)
Sub ShowProgress(sComment)
WScript.Echo sComment
End Sub
OOps, way too late :-)
The 'trick' is really to find the 'pattern', and then to match on this for any given input.