dplsr
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!
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!
Sample....
Private Function CatchMe(ByVal sMyStr as string)
Return sMyStr.Substring( sMyStr.Substring.IndexOf(" g"),sMyStr .Substring .IndexOf(" p"))
End Function
Private Function CatchMe(ByVal sMyStr as string)
Return sMyStr.Substring( sMyStr.Substring.IndexOf("
End Function
sMyStr = k300k400g56ypmk90xx
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("
End Function
sMyStr = (ds.Tables(0).Rows(0)("req
response.write CatchMe(sMyStr)
ASKER
in this case smystr = K-302-K,K-306-KS,K-401-K,K
Are you using VB.net or web app?
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.In dexOf("g") ,sMyStr.In dexOf("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.Substrin g(sMyStr.I ndexOf("g" ),sMyStr.L astIndexOf ("p"))
Use the substring function substring wants the start Index and the end Index
so sMyStr.Substring(sMyStr.In
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.Substrin
ASKER
i am using vb on a asp.net aspx page.
Are the Vars in a Textbox on the page?
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,g70 0p, now i will use the 700 in another query.
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
ASKER
I am getting that error without calling the function.
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(Configuratio nSettings. AppSetting s("Connect ionString" ))
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)("req valve"))
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()
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("
End Function
sub Page_Load(Sender as Object, e as EventArgs)
Dim objConn As SqlConnection = New SqlConnection(Configuratio
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)("req
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.
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.
ASKER
yep, i just stuck this together quick to get the string thing going, thanks
ASKER
instead of the function io tried this:
dim vargroup as string = sMyStr.Substring( sMyStr.Substring.IndexOf("
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
dim vargroup as string = sMyStr.Substring( sMyStr.IndexOf("g"),sMyStr
Andrew
ASKER
Hi andrew!
dim vargroup as string = sMyStr.Substring(sMyStr.In dexOf("g") ,sMyStr.In dexOf("p") )
returns an error of:
System.ArgumentOutOfRangeE xception: StartIndex cannot be less than zero. Parameter name: startIndex
do i need a startwith and endwith somewhere maybe?
dim vargroup as string = sMyStr.Substring(sMyStr.In
returns an error of:
System.ArgumentOutOfRangeE
do i need a startwith and endwith somewhere maybe?
Which means it didnt find "g" in the string.
Andrew
Andrew
try
If sMyStr.IndexOf("g")>=0 AndAlso sMyStr.IndexOf("p") >=0 then
dim vargroup as string = sMyStr.Substring(sMyStr.In dexOf("g") ,sMyStr.In dexOf("p") )
Else
dim vargroup as string ="" ' No match found
End IF
If sMyStr.IndexOf("g")>=0 AndAlso sMyStr.IndexOf("p") >=0 then
dim vargroup as string = sMyStr.Substring(sMyStr.In
Else
dim vargroup as string ="" ' No match found
End IF
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
Line 39: MsgBox(testString.Substrin g(startPos , length))
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(startCh ar)
If startPos < 0 Then
MsgBox("Failed to locate find the first character")
Exit Sub
End If
endPos = startPos + testString.Substring(start Pos).LastI ndexOf(end Char)
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)
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(startCh
If startPos < 0 Then
MsgBox("Failed to locate find the first character")
Exit Sub
End If
endPos = startPos + testString.Substring(start
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
Change this:
length = endPos - startPos
to
length = (endPos - startPos) - 1
Andrew
length = endPos - startPos
to
length = (endPos - startPos) - 1
Andrew
ASKER
cool thanks! andrew, I am going to award some points to arif_eqbal too.
thanks to you both!
thanks to you both!
No Problem...Whatever is appropriate.
Andrew
Andrew
ASKER