Parsing string variable in vbscript

ne0-reloaded

[H]ard|Gawd
Joined
Jul 1, 2003
Messages
1,216
I'm trying to parse a string variable so that it only returns everything before and after certain characters. heres an example"

strVar = "See-Tom-Run"

all i want to return is "Tom"

what ive done so far is this:

Code:
strVar1 = "See-Tom-Run"
strVar2 = Mid(strVar1, 4)

this returns "Tom-Run" but im not sure what function to use parse everything after the second hyphen. Ive tried the left and right function, but i cant get it to work the way i want it to

if anyone can help me out id appreciate it

thanks!
 
Question, what if the string has two of the same character?

"See-Tom-Run" has two hypens... are you assuming to always go from the left?
 
Bear in mind I'm still learning VB... so.......

assuming your var is a fixed length.....

Code:
Function middle(strvar As String)

    middle = Mid(strvar, 5, 3)

End Function

That would return "Tom" if "strvar" was "See-Tom-Run"....

or you could do Mid(strvar,9,3) ro return Run

but you wouldn't need a VB function to do it, Excel knows MID......


if you want to search for a hyphen.......... its a little more complicated....


Keep on Folding!! For the [H]orde!!

 
Here's a dynamic solution:

Code:
<% dim str
str = "See-Tom-Run"

' Returns first character position of the first dash
mod1 = InStr(str,"-")

'mod2 eliminates all characters (including the first dash) before the dash
mod2 = right(str,(len(str)-mod1))

'mod3 figures out the string position before the remaining dash in the mod2 string
mod3 = InStr(mod2,"-") - 1

'finalmod gives you what's left after parsing out the 2nd dash
finalmod = left(mod2,mod3)

response.write "<strong>Original String:</strong> " & str & "<br>"

response.write "<strong>Final String:</strong> " & finalmod
%>
 
Back
Top