Write a C# program to find the find a particular string among the comma seperated strings

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1042
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[] { "," };

objCar
.ForEach(i =>
{
if (i.CarNames
.Split(stringSeparator, StringSplitOptions.None)
.Any(j => j.StartsWith(searchString)))
{
Console.WriteLine("The search string {0} is present in {1} where the ID is {2}", searchString, i.CarNames, i.ID);
}
});
Console.ReadKey();
}
}

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

Comments or Responses

Login to post response