Search code examples
c#stringcomparecontains

How to compare inexact string values


I want to compare two string values which are not exact For example I want to compare Admin to Administrator, this should return true or should execute. I tried contain which is not working

 var prodcut = lstProducts.Where(i => i.Name.ToLower().Contains(appname.ToLower())).FirstOrDefault();

Above code not working if i.Name is 'Admin' and appname.ToLower() is 'Administrator'. It just return null but want it should detect values.


Solution

  • If you want to check it both ways so if A contains B OR if B contains A you can use the || operator (the OR operator) like so:

    a.Contains(b) || b.Contains(a)