Skip to content

Latest commit

 

History

History
18 lines (12 loc) · 864 Bytes

prefer-string-slice.md

File metadata and controls

18 lines (12 loc) · 864 Bytes

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

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.

This rule is partly fixable.

Fail

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

Pass

foo.slice(beginIndex, endIndex);