prefer-string-slice

March 27, 2026 ยท View on GitHub

๐Ÿ“ Prefer String#slice() over String#substr() and String#substring().

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

String#substr() and String#substring() are the two lesser known legacy ways to slice a string. It's better to use String#slice() as it's a more popular option with clearer behavior that has a consistent Array counterpart.

Examples

// โŒ
foo.substr(start, length);

// โŒ
foo.substring(indexStart, indexEnd);

// โœ…
foo.slice(beginIndex, endIndex);