Skip to content

Latest commit

 

History

History
23 lines (15 loc) · 1.2 KB

prefer-string-slice.md

File metadata and controls

23 lines (15 loc) · 1.2 KB

Prefer String#slice() over String#substr() and String#substring()

💼 This rule is enabled in the ✅ recommended config.

🔧 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.

Fail

foo.substr(start, length);
foo.substring(indexStart, indexEnd);

Pass

foo.slice(beginIndex, endIndex);