Solved

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

Posted on 2004-10-29
220 Views
Last Modified: 2010-04-24
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!
0
Question by:dplsr
    27 Comments
     

    Author Comment

    by:dplsr
    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
     
    LVL 27

    Expert Comment

    by:planocz
    Sample....

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

    End Function
    0
     
    LVL 27

    Expert Comment

    by:planocz
    sMyStr =  k300k400g56ypmk90xx
    0
     

    Author Comment

    by:dplsr

    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
     

    Author Comment

    by:dplsr

    in this case smystr = K-302-K,K-306-KS,K-401-K,K-400-K,g700p
    0
     
    LVL 27

    Expert Comment

    by:planocz
    Are you using VB.net or web app?
    0
     
    LVL 19

    Expert Comment

    by: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"))
    0
     

    Author Comment

    by:dplsr
    i am using vb on a asp.net  aspx page.  
    0
     
    LVL 27

    Expert Comment

    by:planocz
    Are the Vars in a Textbox on the page?
    0
     

    Author Comment

    by:dplsr
    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
     

    Author Comment

    by:dplsr

    I am getting that error without calling the function.
    0
     

    Author Comment

    by:dplsr
    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
     
    LVL 27

    Expert Comment

    by:planocz
    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
     

    Author Comment

    by:dplsr
    yep, i just stuck this together quick to get the string thing going, thanks
    0
     

    Author Comment

    by:dplsr


    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
     
    LVL 4

    Expert Comment

    by:andrewharris
    Try

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

    Andrew
    0
     

    Author Comment

    by:dplsr
    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
     
    LVL 4

    Expert Comment

    by:andrewharris
    Which means it didnt find "g" in the string.

    Andrew
    0
     
    LVL 19

    Expert Comment

    by:arif_eqbal
    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
     
    LVL 4

    Assisted Solution

    by:andrewharris
    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
     

    Author Comment

    by:dplsr
    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
     

    Author Comment

    by:dplsr
    Line 39:         MsgBox(testString.Substring(startPos, length))
    0
     
    LVL 19

    Accepted Solution

    by:
    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
     

    Author Comment

    by:dplsr
    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
     
    LVL 4

    Expert Comment

    by:andrewharris
    Change this:

    length = endPos - startPos

    to

    length = (endPos - startPos) - 1

    Andrew
    0
     

    Author Comment

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

    thanks to you both!
    0
     
    LVL 4

    Expert Comment

    by:andrewharris
    No Problem...Whatever is appropriate.

    Andrew
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    Suggested Solutions

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now