Link to home
Start Free TrialLog in
Avatar of dplsr
dplsrFlag for Afghanistan

asked on

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!
Avatar of dplsr
dplsr
Flag of Afghanistan image

ASKER

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
Avatar of Howard Cantrell
Sample....

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

End Function
sMyStr =  k300k400g56ypmk90xx
Avatar of dplsr

ASKER


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)
Avatar of dplsr

ASKER


in this case smystr = K-302-K,K-306-KS,K-401-K,K-400-K,g700p
Are you using VB.net or web app?
Avatar of arif_eqbal
arif_eqbal

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"))
Avatar of dplsr

ASKER

i am using vb on a asp.net  aspx page.  
Are the Vars in a Textbox on the page?
Avatar of dplsr

ASKER

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.
Avatar of dplsr

ASKER


I am getting that error without calling the function.
Avatar of dplsr

ASKER

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()
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.
Avatar of dplsr

ASKER

yep, i just stuck this together quick to get the string thing going, thanks
Avatar of dplsr

ASKER



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?
Try

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

Andrew
Avatar of dplsr

ASKER

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?
Which means it didnt find "g" in the string.

Andrew
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

SOLUTION
Avatar of andrewharris
andrewharris

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dplsr

ASKER

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.
Avatar of dplsr

ASKER

Line 39:         MsgBox(testString.Substring(startPos, length))
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dplsr

ASKER

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)
Change this:

length = endPos - startPos

to

length = (endPos - startPos) - 1

Andrew
Avatar of dplsr

ASKER

cool thanks! andrew, I am going to award some points to arif_eqbal  too.

thanks to you both!
No Problem...Whatever is appropriate.

Andrew