Avatar of Valleriani
Valleriani
Flag for Sweden 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!
Visual Basic ClassicVB Script

Avatar of undefined
Last Comment
Krys_K

8/22/2022 - Mon
peetm

Are the words 'here with you' always present?  It obviously works for a group, and/or an individual.  And, is this list of companions always last?  What's the 'pattern'?

The 'trick' is really to find the 'pattern', and then to match on this for any given input.
peetm

Oh, >>is this list of companions always last?

Apart from ...

>>There is a staff on the floor.




Todd Gerbert

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Valleriani

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.
ASKER CERTIFIED SOLUTION
peetm

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Valleriani

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.
Valleriani

ASKER
Oh cool thanks, will try that one!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Valleriani

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?
SOLUTION
Todd Gerbert

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Valleriani

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!
peetm

Yes, replace !? with a period before

   ' Split into sentences.
    '
    sa1 = Split(s, ".")



Your help has saved me hundreds of hours of internet surfing.
fblack61
Krys_K

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

	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

Open in new window

Krys_K

OOps, way too late :-)