C#

C#

By DeepSource

Consider using the range operator `..` over `Substring`CS-R1037

Anti-pattern

String.Substring takes parameters such as the starting index and/or length and returns a part of the specified string. However, this entire expression can be simplified using the range operator, i.e., the .. operator.

Examples

Bad Practice

var str = "CSharp";
var part = str.Substring(1, 3);

Recommended

var str = "CSharp";
var part = str[1..4];

Reference