Skip to content

Latest commit

Β 

History

History
25 lines (16 loc) Β· 1.27 KB

File metadata and controls

25 lines (16 loc) Β· 1.27 KB

prefer-string-slice

πŸ“ 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);