I have list of strings. At some point I need to check if list contains some string.
For example:
public List<string> Headers { get; private set; }
Headers = new List<string>();
Headers.Add("bla1 bla1 ");// pay attention to the whitespace at the end
Headers.Add("bla2 bla2");
Headers.Add("bla3 bla3");
Headers.Add("bla4 bla4");
At some point I need to check if list of string contains for example "bla1 bla1"
string.
For this purpose I use this line of code:
var isExists = Headers
.Where(h =>!h.Equals("bla1 bla1", StringComparison.InvariantCultureIgnoreCase)
But the result of isExists
variable is false because headers contains "bla1 bla1"
string with a space at the end.
How can I ignore whitespaces
at the start and at the end of the string?
Trim()
on value before adding it toHeaders
).