ASP String Manipulation Question Using inStr()

Hello Experts-

I have a function that returns me the portion of a string before the first hyphen:

Function createRewriteFolder (text)
      pos=InStr(text,"-")
      If pos = 0 Then
      createRewriteFolder = text
      Else
      ' Takes everything before fist hyphen, and subtracts 1 to lose the hyphen
      createRewriteFolder = Left(text, InStr(text, "-")-1)
      End If
End Function

A string like "this-is-a-string" would return "this".

I am wondering how to expand this function to take everything before the second hyphen. That is, return "thisis". Any ideas on how this can be done?

- CJ
LVL 3
cjasonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jitgangulyCommented:
abc=split("this-is-a-string","-")

xyz= abc(0)&abc(1)
jitgangulyCommented:
This is better

abc=split("this-is-a-string","-")

for i = 0 to ubound(abc)
xyz= xyz + abc(i)
next

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nschaferCommented:
Somthing like this should work

Function createRewriteFolder (text)
     pos=InStr(text,"-")
     If pos = 0 Then
     createRewriteFolder = text
     Else
     ' Takes everything before fist hyphen, and subtracts 1 to lose the hyphen
       v= Left(text, InStr(text, "-")-1)
       text = mid(text,inStr(test,"-"),len(text))
       v = v & Left(text, InStr(text, "-")-1)
       createRewriteFolder = v
     End If
End Function

Neal
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

nschaferCommented:
nschafer,
  The split method mentioned by jitganguly would be better than my method, I didn't refresh before posting.

Neal.
jitgangulyCommented:
Yes indeed. Thanks you are so humble Neal :-)
cjasonAuthor Commented:
Works perfectly, thanks for your help!
zapthedingbatCommented:
or if you want to retain the first "-" then you could use a couple of nested instr()s like this:

Dim sText: sText = "this-is-a-string"
Left(sText, Instr(Instr(1, sText, "=")+1, sText, "=")+1)

which should return "this-is". but its not pretty personally i'd opt for a split  based method like jitganguly's.

But the single line of code solution award goes to...

Split(Replace("this-is-a-string","-","",1,1), "-")(0)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.