Methods such as string.Contains
and string.IndexOf
allow you to search for an occurrence of either a single char or a substring within a string
. If you'd like to search for an occurrence of a single char, it is recommended that you use the appropriate method that takes a char
as a parameter rather than a string
as the former approach is more performant and recommended.
var lang = "csharp";
// Searching for `a` using the method that takes a string as a parameter.
// Although this works, it is not an efficient approach.
var idx = lang.IndexOf("a");
var lang = "csharp";
// Searching for `a` using the method that takes a Char as a parameter.
// This is the recommended performant approach.
var idx = lang.IndexOf('a');