How do I capture all of the charachters between 2 known charachters in a string and assign them to a variable?

In .net using VB

I need to capture all of the charachters between 2 known charachters and assign the results to a variable.

Another words, in the string   k300k400g56ypmk90xx I want to capture everything between the g and the p and assign it to say the variable myvar. the g and p do not have to be included, but it's also fine if they are

thanks!
dplsrAsked:
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.

dplsrAuthor Commented:
The boundry charachters will always be the same in the database(this string will be a field from a database. I can make them anything, xx, zz etc
0
planoczCommented:
Sample....

Private Function CatchMe(ByVal sMyStr as string)
 
     Return   sMyStr.Substring( sMyStr.Substring.IndexOf("g"),sMyStr.Substring.IndexOf("p"))

End Function
0
planoczCommented:
sMyStr =  k300k400g56ypmk90xx
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

dplsrAuthor Commented:

Ok I am getting error: BC30516: Overload resolution failed because no accessible 'Substring' accepts this number of arguments.


here is my code:

 Private Function CatchMe(ByVal sMyStr as string)
 
     Return   sMyStr.Substring( sMyStr.Substring.IndexOf("g"),sMyStr.Substring.IndexOf("p"))

End Function

 sMyStr  = (ds.Tables(0).Rows(0)("reqvalve"))

response.write CatchMe(sMyStr)
0
dplsrAuthor Commented:

in this case smystr = K-302-K,K-306-KS,K-401-K,K-400-K,g700p
0
planoczCommented:
Are you using VB.net or web app?
0
arif_eqbalCommented:
I hope you undesrtand what planocz has suggested

Use the substring function substring wants the start Index and the end Index
so sMyStr.Substring(sMyStr.IndexOf("g"),sMyStr.IndexOf("p")) will return what you want

But there's One issue in case your String has more than one "g" or more than one "p" it will only return the string between the first "g" and the first "p"

In case you want that it should get you value between first occurance of "g" and the last of "p" then


MyVariable=sMyStr.Substring(sMyStr.IndexOf("g"),sMyStr.LastIndexOf("p"))
0
dplsrAuthor Commented:
i am using vb on a asp.net  aspx page.  
0
planoczCommented:
Are the Vars in a Textbox on the page?
0
dplsrAuthor Commented:
nope, this is all in the <script> section
I simple need to capture the value between the g and p and the use that value in another database  select query. I used response.write to test the function

I am not sure if i wrote it correct?

in tis case my 1st  query returned  K-302-K,K-306-KS,K-401-K,K-400-K,g700p, now i will use the 700 in another query.
0
dplsrAuthor Commented:

I am getting that error without calling the function.
0
dplsrAuthor Commented:
my code will look like this, if your function will work. can I call a function from the <script >
area. Sorry I am not exactly a newbie, but close to it.


Protected sMyStr as string

 Private Function CatchMe(ByVal sMyStr as string)
 
     Return   sMyStr.Substring( sMyStr.Substring.IndexOf("g"),sMyStr.Substring.IndexOf("p"))

End Function

  sub Page_Load(Sender as Object, e as EventArgs)
 
              Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

      dim objCmd as SqlDataAdapter = new SqlDataAdapter _
        ("select reqvalve from Products where modelnumber like 'K-922af' ", objConn)

dim ds as dataset = new Dataset()
objCmd.Fill(ds, "CMRC_Products")

 sMyStr  = (ds.Tables(0).Rows(0)("reqvalve"))
 
 dim vargroup as string = CatchMe(sMyStr)
 
      dim objCmd2 as SqlDataAdapter = new SqlDataAdapter _
        ("select * from Products where family like '"& vargroup &"' ", objConn)

dim ds2 as dataset = new Dataset()
objCmd2.Fill(ds2, "CMRC_Products")
MyDataList.DataSource = ds2.Tables("CMRC_Products"). _
DefaultView
MyDataList.Databind()
0
planoczCommented:
you could also just use one dataset....

dim ds2 as dataset = new Dataset()
objCmd2.Fill(ds2, "CMRC_Products")

change to

objCmd.Fill(ds, "CMRC_Products2")
then you can work with ds.Tables(1) as your second table in the dataset.
0
dplsrAuthor Commented:
yep, i just stuck this together quick to get the string thing going, thanks
0
dplsrAuthor Commented:


instead of the function io tried this:

  dim vargroup as string = sMyStr.Substring( sMyStr.Substring.IndexOf("g"),sMyStr.Substring.IndexOf("p"))

i get the same error: BC30516: Overload resolution failed because no accessible 'Substring' accepts this number of arguments.

what about a regular expression?
0
andrewharrisCommented:
Try

dim vargroup as string = sMyStr.Substring( sMyStr.IndexOf("g"),sMyStr.IndexOf("p"))

Andrew
0
dplsrAuthor Commented:
Hi andrew!

 dim vargroup as string = sMyStr.Substring(sMyStr.IndexOf("g"),sMyStr.IndexOf("p"))
returns an error of:
System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. Parameter name: startIndex

do i need a startwith and endwith somewhere maybe?
0
andrewharrisCommented:
Which means it didnt find "g" in the string.

Andrew
0
arif_eqbalCommented:
try

If sMyStr.IndexOf("g")>=0 AndAlso sMyStr.IndexOf("p") >=0 then
dim vargroup as string = sMyStr.Substring(sMyStr.IndexOf("g"),sMyStr.IndexOf("p"))
Else
dim vargroup as string ="" ' No match found
End IF

0
andrewharrisCommented:
Try this
        Dim testString As String
        Dim startPos As Integer
        Dim endPos As Integer
        Dim startChar As Char
        Dim endChar As Char
        Dim length As Integer

        startChar = "k"
        endChar = "d"

        testString = "wdertyuikjnhvfdrtyh"
        startPos = testString.IndexOf(startChar)
        If startPos < 0 Then
            MsgBox("Failed to locate find the first character")
            Exit Sub
        End If

        endPos = startPos + testString.Substring(startPos).LastIndexOf(endChar)

        If endPos < startPos Then
            MsgBox("Failed to locate find the last character")
            Exit Sub
        End If

        length = endPos - startPos
        startPos += 1

        MsgBox(testString.Substring(startPos, length))

Andrew
0
dplsrAuthor Commented:
Goodmornin andrew!

I tried the testing code. I get error:

It is invalid to show a modal dialog or form when the application is not running in UserInteractive mode. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
0
dplsrAuthor Commented:
Line 39:         MsgBox(testString.Substring(startPos, length))
0
arif_eqbalCommented:
Well he is just giving a sample I think you need to replace Msgbox with something more meaningful for you app like displaying th result in a textBox.......

0

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
dplsrAuthor Commented:
ok

I changed the test code to the follwing, and added a asp:label. It returned  jnhvfd . Cool! now my question is, If the start was k and the 1st charachter returned was j, the end charachter was d, butthe last charachter returned was d. why was the last charachter returned not f or the 1st returned not k?  

thanks!

Dim testString As String
        Dim startPos As Integer
        Dim endPos As Integer
        Dim startChar As Char
        Dim endChar As Char
        Dim length As Integer

        startChar = "k"
        endChar = "d"

        testString = "wdertyuikjnhvfdrtyh"
        startPos = testString.IndexOf(startChar)
        If startPos < 0 Then
            MsgBox("Failed to locate find the first character")
            Exit Sub
        End If

        endPos = startPos + testString.Substring(startPos).LastIndexOf(endChar)

        If endPos < startPos Then
            lblMsgBox.text="Failed to locate find the last character"
            Exit Sub
        End If

        length = endPos - startPos
        startPos += 1

        lblMsgBox.text=(testString.Substring(startPos, length)
0
andrewharrisCommented:
Change this:

length = endPos - startPos

to

length = (endPos - startPos) - 1

Andrew
0
dplsrAuthor Commented:
cool thanks! andrew, I am going to award some points to arif_eqbal  too.

thanks to you both!
0
andrewharrisCommented:
No Problem...Whatever is appropriate.

Andrew
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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.