locked
Array question RRS feed

  • Question

  • User135331403 posted

    I have an string of numbers I want to see if a number is in the string. Do I put it in an array? What is the best way to do this

    I know this does not work, but you get the idea???

    Dim stringVal = "50, 20, 32, 19, 29"
    Dim numbers() As Integer = {Int(stringVal)}
    Dim index As Integer = Array.IndexOf(numbers, 19) 'returns 3

    Monday, August 18, 2008 11:54 AM

All replies

  • User-417784260 posted
    Remember the first element in an array is at position 0 so the 4th element is at position 3
    Monday, August 18, 2008 12:47 PM
  • User135331403 posted
    Yes. I know, but it bows up at:

    Dim numbers() As Integer = {Int(stringVal)}

    I get: Conversion from string "50, 20, 32, 19, 29" to type 'Double' is not valid.

    Monday, August 18, 2008 12:54 PM
  • User-417784260 posted

     If you want to convert to integers you would have to do something like this. 

            Dim stringVal = "50, 20, 32, 19, 29"
            Dim strs() = stringVal.Split(","c)
            Dim numbers() As Integer
            numbers = Array.ConvertAll(strs, New Converter(Of String, Integer)(AddressOf StringToNum))
            Debug.Print(Array.IndexOf(numbers, 19).ToString)
        End Sub
    
        Public Function StringToNum(ByVal s As String) As Integer
            Return CInt(s)
        End Function
     
    Monday, August 18, 2008 1:58 PM
  • User-1234877047 posted

    No need of an array if you want to find that a number is in a string

    Try this

    Dim stringVal = "50, 20, 32, 19, 29"

    Console.writeLine(stringVal.ToString().Contains("50"))

    .contains return boolean, if it finds then true is returned else it returns false

    .

    Wednesday, August 27, 2008 6:06 AM