I need to get a 4/5 digit number which always comes directly after a #
For example it'll be "Item Title (#1234)" however it's not always in the same place or at the end.
Not sure how I go about doing that
You need to manually parse the string.
I've made a quick function for this:
'//return the number after delimeter string, if exists (first occurance only)
'//in case no number exists, returns Empty value
Function FindNumberAfter(rawValue, delimeterString)
Dim index, x, curChar
Dim sBuffer
FindNumberAfter = vbEmpty
index = InStr(rawValue, delimeterString)
If index>0 Then
For x=index+Len(delimeterString) To Len(rawValue)
curChar = Mid(rawValue, x, 1)
If IsNumeric(curChar) Then
sBuffer = sBuffer & curChar
Else
Exit For
End If
Next
If Len(sBuffer)>0 Then FindNumberAfter = CLng(sBuffer)
End If
End Function
Usage in your case:
Response.Write(FindNumberAfter("Item Title (#1234)", "#"))
Related
How can I access the name property of a ProtoField after I declare it?
For example, something along the lines of:
myproto = Proto("myproto", "My Proto")
myproto.fields.foo = ProtoField.int8("myproto.foo", "Foo", base.DEC)
print(myproto.fields.foo.name)
Where I get the output:
Foo
An alternate method that's a bit more terse:
local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* myproto")
print(string.sub(fieldString, i + 2, j - (1 + string.len("myproto")))
EDIT: Or an even simpler solution that works for any protocol:
local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* ")
print(string.sub(fieldString, i + 2, j - 1))
Of course the 2nd method only works as long as there are no spaces in the field name. Since that's not necessarily always going to be the case, the 1st method is more robust. Here is the 1st method wrapped up in a function that ought to be usable by any dissector:
-- The field is the field whose name you want to print.
-- The proto is the name of the relevant protocol
function printFieldName(field, protoStr)
local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* " .. protoStr)
print(string.sub(fieldString, i + 2, j - (1 + string.len(protoStr)))
end
... and here it is in use:
printFieldName(myproto.fields.foo, "myproto")
printFieldName(someproto.fields.bar, "someproto")
Ok, this is janky, and certainly not the 'right' way to do it, but it seems to work.
I discovered this after looking at the output of
print(tostring(myproto.fields.foo))
This seems to spit out the value of each of the members of ProtoField, but I couldn't figure out the correct way to access them. So, instead, I decided to parse the string. This function will return 'Foo', but could be adapted to return the other fields as well.
function getname(field)
--First, convert the field into a string
--this is going to result in a long string with
--a bunch of info we dont need
local fieldString= tostring(field)
-- fieldString looks like:
-- ProtoField(188403): Foo myproto.foo base.DEC 0000000000000000 00000000 (null)
--Split the string on '.' characters
a,b=fieldString:match"([^.]*).(.*)"
--Split the first half of the previous result (a) on ':' characters
a,b=a:match"([^.]*):(.*)"
--At this point, b will equal " Foo myproto"
--and we want to strip out that abreviation "abvr" part
--Count the number of times spaces occur in the string
local spaceCount = select(2, string.gsub(b, " ", ""))
--Declare a counter
local counter = 0
--Declare the name we are going to return
local constructedName = ''
--Step though each word in (b) separated by spaces
for word in b:gmatch("%w+") do
--If we hav reached the last space, go ahead and return
if counter == spaceCount-1 then
return constructedName
end
--Add the current word to our name
constructedName = constructedName .. word .. " "
--Increment counter
counter = counter+1
end
end
Running VS 2013 VB.net.
I have 3 labels on my aspx page labelled label1, label2 and label3.
I want to loop through each one in the code behind and assign values to them.
Here is my code
Dim X As Integer = 1
For Each obj In values
Dim myLabel As Label
myLabel = TryCast(Me.FindControl("Label" + X), Label)
myLabel.Text = Math.Round(obj, 2)
X = X + 1
Next
I know there is only 3 obj's so x will always be between 1 and 3. What am I doing wrong as I get the following.
Conversion from string "Label" to type 'Double' is not valid
IF I change ("label" + x) to ("label1") I get
Object reference not set to an instance of an object.
on the line below.
You want to concatenate strings not to calculate, in VB.NET you use & instead of +(as opposed to C#). So this should work:
myLabel = TryCast(Me.FindControl("Label" & X), Label)
You should also set Option Strict to On, without exception. Then you first have to fix a lot of compiler errors, but it will help to write much more robust code. This wouldn't compile either:
myLabel.Text = Math.Round(obj, 2)
because a Math.Round returns a Double and Label.Text is a String. You just need to use Math.Round(obj, 2).ToString() to fix it.
Here's a different approach to get your labels using Linq:
Dim myLabels = From lbl In Me.Form.Controls.OfType(Of Label)()
Where lbl.ID.StartsWith("Label")
For Each lbl As Label In myLabels
' ... '
Next
Also since your using TryCast check your variable before using it - it will be Nothing if it does not make the cast.
myLabel = TryCast(Me.FindControl("Label" & X.ToString), Label)
If myLabel IsNot Nothing Then
'safe to use variable here
End If
I have tried all but its not working
I am getting a colum value form Data base as
substatus=rsprefobj("isnotificationactive");
after doing
Response.write substatus
It gives me --> n
and when i do
intcomaprestringval=StrComp(substatus,"n",vbTextCompare)
Response.write intcomaprestringval
It(intcomaprestringval) gives me --> 1 even though they are same
I want to take some decision based on database value if its "n" or "y"
If intcomaprestringval = 0 Then
some
Else
some
End If
But StrComp()always returns 1 in my case whether database value is "n" or "y" :(
I write a compare function that will return either "OK" if 2 values are a match or "No Match" if they do not. Here it is:
Function Compare(str1, str2, comp)
str = "OK"
If StrComp(str1, str2, comp) <> 0 Then str = "No Match"
Compare = str
End Function
str1 and str2 are the 2 values you wish to compare and comp is the method of comparison (0 = binary comparison and 1 = text comparison). I always use 0.
So you would use it like this:
If Compare(substatus, "n", 0) = "OK" Then
' Values match
Else
'Values do not match
End If
Hope it helps
Regarding your test:
intcomaprestringval=StrComp(substatus,"n",vbTextCompare)
Are you setting vbTextCompare to be 0 at some point earlier in your code?
I have used Trim() as suggested by #EntBark
Dim myvalue
myvalue=Trim(substatus)
intcomaprestringval=StrComp(myvalue,"n",vbTextCompare)
This is my code.
Public Sub SomeFucntion(ByVal test As Short)
SomeOtherFucntion(MyArray)
If Array.IndexOf(MyArray, test) <> -1
//....
End If
End Sub
test return value 10.
But i am getting IndexOf value as -1.
Inside QuickWatch in VS 2005 when i put the value as 10 instead of test i am getting the correct index.
My array is single dimensional array which now has 2,5 and 10. Since its got 10 it should ideally return 2 as the index.
The problem might be caused by the fact that your array contains integers, but the value you are looking for is a short. Consider the following example:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, value)) ' Prints -1
If the array contains integers, you need to convert your short into an integer first, for example, by using CInt:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' Prints 0
Edit: Note that the declared type has nothing to do with this. Let's declare the array as Object, since that's what you mentioned in your comment (note that the following example requires Option Strict Off, which is bad):
Dim myArray As Object = New Integer() {5}
Dim value As Object = 5S ' Short literal
Console.WriteLine(Array.IndexOf(myArray, value)) ' still returns -1
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' returns 0
Note: You can make that conversion implicit by declaring your function as Public Sub SomeFunction(ByVal test As Integer).
Dim test As Short = 5
Dim MyArray() As Short = {1, 3, 4, 5, 7, 3}
If Array.IndexOf(MyArray, test) <> -1 Then
MessageBox.Show("Index Found.")
End If
The above works. since test is declared as short, make sure MyArray is also declared as short.
What i want is to retrieve quantity in database from piece and covert it to dozen. Then input as dozen and convert back to pieces and save to database again.
when I input data eg. 10.3, it should convert to 123 piece for me ((10 * 12) + 3). My code work well without my "If clause" but only when data was "single" type. It made error when I input integer number, so I added "If.." statement to check it first which is now the output was correct for Integer but incorrect when I input single number.
I have this code..
Function DzToPcs(val)
'If CLng(val) = val then <-- not work
'if Fix(val) <> val then <-- work but the output was not correct when input single type number.
if Int(vInt) = vInt then <-- work but the output was not correct when input single type number.
DztoPcs = val * 12
else
strInt = Cstr(val)
a = Split(strInt,".")
dz = a(0)
pcs = a(1)
getdz = Cint(dz)
getpcs = Cint(pcs)
DztoPcs = (getdz * 12) + getpcs
end if
I'm not sure what's wrong with your if statements (my VBScript is a little rusty), but you could try this alternative:
Function DzToPcs(val)
strInt = Cstr(val)
a = Split(strInt,".")
dz = a(0)
if UBound(a) > 0 then
pcs = a(1)
getdz = Cint(dz)
getpcs = Cint(pcs)
DztoPcs = (getdz * 12) + getpcs
else
DztoPcs = dz * 12
end if
end function