Skip to content

Latest commit

 

History

History
20 lines (12 loc) · 861 Bytes

prefer-string-slice.md

File metadata and controls

20 lines (12 loc) · 861 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 fixible when no arguments are passed to the method.

Fail

foo.substr(1, 2);
foo.substring(1, 3);

Pass

foo.slice(1, 3);