Write a VB.net program to find the find the position of a particular string among the comma separated strings using FindIndex

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 227
Imports System.Collections.Generic
Imports System.Linq

Namespace ConsoleApplication3
Class Program
Private Shared Sub Main(args As String())
Dim objCar As New List(Of Car)()
objCar.Add(New Car() With { _
Key .ID = 1, _
Key .CarNames = "Finch,Pellandini,Kremer,Studebaker" _
})
objCar.Add(New Car() With { _
Key .ID = 2, _
Key .CarNames = "Theologou,Premier,REVA,Chinkara,Ferrari" _
})
objCar.Add(New Car() With { _
Key .ID = 3, _
Key .CarNames = "Porsche,Mercedes Benz" _
})
objCar.Add(New Car() With { _
Key .ID = 4, _
Key .CarNames = "Volkswagen,Audi,Finch,BMW" _
})
objCar.Add(New Car() With { _
Key .ID = 5, _
Key .CarNames = "Lamborghini,Fiat,Maserati,Honda" _
})
objCar.Add(New Car() With { _
Key .ID = 6, _
Key .CarNames = "Proton,Finch,Tan Chong Motor,VAM" _
})

Dim searchString As String = "Finch"
Dim stringSeparator As String() = New String() {","}

For Each x As var In objCar
Dim idx = x.CarNames.Split(stringSeparator, StringSplitOptions.None).ToList().FindIndex(Function(f) f.Contains(searchString))
If idx >= 0 Then

Console.WriteLine("The search string {0} is present in {1} at position {2}", searchString, x.CarNames, idx + 1)
End If
Next

Console.ReadKey()
End Sub
End Class

Public Class Car
Public Property ID() As Integer
Get
Return m_ID
End Get
Set
m_ID = Value
End Set
End Property
Private m_ID As Integer
Public Property CarNames() As String
Get
Return m_CarNames
End Get
Set
m_CarNames = Value
End Set
End Property
Private m_CarNames As String
End Class
End Namespace

Comments or Responses

Login to post response