[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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
27
Medium Priority
?
232 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
Comment
Question by:dplsr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 5
  • 5
  • +1
27 Comments
 

Author Comment

by:dplsr
ID: 12446960
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
ID: 12446993
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
ID: 12447000
sMyStr =  k300k400g56ypmk90xx
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dplsr
ID: 12447318

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
ID: 12447339

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

Expert Comment

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

Expert Comment

by:arif_eqbal
ID: 12447393
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
ID: 12447394
i am using vb on a asp.net  aspx page.  
0
 
LVL 27

Expert Comment

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

Author Comment

by:dplsr
ID: 12447505
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
ID: 12447546

I am getting that error without calling the function.
0
 

Author Comment

by:dplsr
ID: 12447738
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
ID: 12447818
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
ID: 12447843
yep, i just stuck this together quick to get the string thing going, thanks
0
 

Author Comment

by:dplsr
ID: 12447981


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
ID: 12449478
Try

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

Andrew
0
 

Author Comment

by:dplsr
ID: 12452983
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
ID: 12453868
Which means it didnt find "g" in the string.

Andrew
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12460586
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
andrewharris earned 1600 total points
ID: 12460733
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
ID: 12462497
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
ID: 12462500
Line 39:         MsgBox(testString.Substring(startPos, length))
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 400 total points
ID: 12463960
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
ID: 12467434
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
ID: 12467511
Change this:

length = endPos - startPos

to

length = (endPos - startPos) - 1

Andrew
0
 

Author Comment

by:dplsr
ID: 12467686
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
ID: 12467724
No Problem...Whatever is appropriate.

Andrew
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 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