Avoid StartsWith or EndsWith with one character

Metadata

ID: csharp-best-practices/strings-with-one-char

Language: C#

Severity: Warning

Category: Performance

Description

When using StartsWith or EndsWith with strings of only one character, prefer using a character type.

Non-Compliant Code Examples

using System.Xml;

class MyClass {
    public static void processString(string s)
    {
        bool r1 = s.StartsWith("/");
        bool r2 = s.EndsWith("/");
        data.Contains("\\n");
    }
}

Compliant Code Examples

using System.Xml;

class MyClass {
    public static void processString(string s)
    {
        bool r1 = s.StartsWith('/');
        bool r2 = s.EndsWith('/');
        
    }
}