Skip to content

noSubstr

诊断类别:lint/nursery/noSubstr

¥Diagnostic Category: lint/nursery/noSubstr

自从:v1.8.2

¥Since: v1.8.2

来源:

¥Sources:

强制使用 String.slice() 而不是 String.substr()String.substring()

¥Enforce the use of String.slice() over String.substr() and String.substring().

String.slice() 优于 String.substr()String.substring(),因为它是一种更受欢迎的选项,行为更清晰,并且在数组中具有一致的对应项。

¥String.slice() is preferred over String.substr() and String.substring() because it is a more popular option with clearer behavior, and it has a consistent counterpart in arrays.

请注意,传递参数时,String.substrString.substringString.slice 并不相同。有关详细差异,请参阅 MDN 文档:

¥Note that String.substr, String.substring and String.slice are not identical when arguments are passed. For detailed differences, refer to the MDN documentation:

¥Examples

¥Invalid

foo.substr();
code-block.js:1:5 lint/nursery/noSubstr  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid using substr and consider using slice instead.

> 1 │ foo.substr();
^^^^^^
2 │

slice is more commonly used and has a less surprising behavior.

See MDN web docs for more details.

Unsafe fix: Use .slice() instead.

1 - foo.substr();
1+ foo.slice();
2 2

foo.substring();
code-block.js:1:5 lint/nursery/noSubstr  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid using substring and consider using slice instead.

> 1 │ foo.substring();
^^^^^^^^^
2 │

slice is more commonly used and has a less surprising behavior.

See MDN web docs for more details.

Unsafe fix: Use .slice() instead.

1 - foo.substring();
1+ foo.slice();
2 2

¥Valid

foo.slice(beginIndex, endIndex);

¥Related links