How to search a string from comma separated string in C#?

Suppose, I have a string like "abc, xyz, abc, efd", now I need to search "abc" from the comma separated string, how to check if it is inside the string or not.

If possible, I would like to know the solution in Linq, thanks

Asked by:- jaiprakash
: 3940 At:- 5/3/2018 12:10:53 PM
C# ASP.NET search string

1 Answers
profileImage Answered by:- vikas_jk

using Linq you can search string as

var SearchString ="abc, xyz, abc, efd";
var ContainsStringOrNot=SearchString.Split(',').Contains("abc");

without Linq, there are several other methods like using Regex, using Array.IndexOf function

Using regex

var SearchString ="abc, xyz, abc, efd";
var match=Regex.Match(SearchString , @"\abc\b");

if (match.Success)
At:- 5/4/2018 6:39:51 PM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use