Write a C# program to find the find the position of a particular string among the comma seperated strings using IndexOf method

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 963
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
List<Car> objCar = new List<Car>();
objCar.Add(new Car { ID = 1, CarNames = "Finch,Pellandini,Kremer,Studebaker" });
objCar.Add(new Car { ID = 2, CarNames = "Theologou,Premier,REVA,Chinkara,Ferrari" });
objCar.Add(new Car { ID = 3, CarNames = "Porsche,Mercedes Benz" });
objCar.Add(new Car { ID = 4, CarNames = "Volkswagen,Audi,Finch,BMW" });
objCar.Add(new Car { ID = 5, CarNames = "Lamborghini,Fiat,Maserati,Honda" });
objCar.Add(new Car { ID = 6, CarNames = "Proton,Finch,Tan Chong Motor,VAM" });

string searchString = "Finch";
string[] stringSeparator = new string[] { "," };

foreach (var x in objCar)
{
var idx = x.CarNames
.Split(stringSeparator, StringSplitOptions.None)
.ToList()
.IndexOf(searchString);
if(idx >=0)
{
Console.WriteLine("The search string {0} is present in {1} at position {2}", searchString, x.CarNames, idx+1);

}
}

Console.ReadKey();
}
}

public class Car
{
public int ID { get; set; }
public string CarNames { get; set; }
}
}

Comments or Responses

Login to post response