Closed BryanWilhite closed 9 months ago
Issue #158 leads us to “Best practices for comparing strings in .NET” and the following points that are relevant to [[Songhay Core (C♯)]]:
- Use overloads that explicitly specify the string comparison rules for string operations. Typically, this involves calling a method overload that has a parameter of type StringComparison.
- Use StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.
- Use comparisons with StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for better performance.
- Use string operations that are based on StringComparison.CurrentCulture when you display output to the user. …
- Use the String.ToUpperInvariant method instead of the String.ToLowerInvariant method when you normalize strings for comparison.
- Use an overload of the String.Equals method to test whether two strings are equal. …
- Don't use string operations based on StringComparison.InvariantCulture in most cases. One of the few exceptions is when you're persisting linguistically meaningful but culturally agnostic data.
StringComparison.OrdinalIgnoreCase
more than any other comparison type.Songhay.Extensions.StringExtensions.EqualsInvariant
is correct with respect to this context.*.IndexOf
calls in [[Songhay Core (C♯)]] is for Array.IndexOf
which has no overloads for string comparison.Songhay.Extensions.StringExtensions.ToSubstringInContext
uses String.IndexOf
with StringComparison.Ordinal
.Songhay.Xml.HtmlUtility.EvaluateOpenElement
uses String.IndexOf
with StringComparison.OrdinalIgnoreCase
.String.Compare
is not explicitly used in [[Songhay Core (C♯)]].String.ToLowerInvariant
is not used in any comparison operations—it is used only to format for display.
🔗 https://learn.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings