Skip to content

stringSliceMethods

Reports usage of substring instead of slice for string operations.

✅ This rule is included in the ts logical presets.

JavaScript provides three methods for extracting substrings: slice(), substr(), and substring(). This rule encourages consistent use of slice() because:

  • substr() is deprecated and removed from the ECMAScript specification
  • substring() has confusing behavior: it auto-swaps arguments when start > end, and treats negative indices as 0
  • slice() has consistent, predictable behavior that matches Array.prototype.slice()
const
const result: string
result
=
const text: string
text
.
String.substr(from: number, length?: number): string

Gets a substring beginning at the specified location and having the specified length.

@deprecatedA legacy feature for browser compatibility

@paramfrom The starting position of the desired substring. The index of the first character in the string is zero.

@paramlength The number of characters to include in the returned substring.

substr
(1, 5);
const
const result: string
result
=
const text: string
text
.
String.substring(start: number, end?: number): string

Returns the substring at the specified location within a String object.

@paramstart The zero-based index number indicating the beginning of the substring.

@paramend Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end. If end is omitted, the characters from start through the end of the original string are returned.

substring
(1, 5);
const
const last: string
last
=
const text: string
text
.
String.substring(start: number, end?: number): string

Returns the substring at the specified location within a String object.

@paramstart The zero-based index number indicating the beginning of the substring.

@paramend Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end. If end is omitted, the characters from start through the end of the original string are returned.

substring
(
const text: string
text
.
String.length: number

Returns the length of a String object.

length
- 3);

This rule is not configurable.

If you’re maintaining legacy code that heavily uses substr() or substring() and refactoring would be too costly, you may need to disable this rule. Note that substr() is deprecated and may be removed from JavaScript engines in the future.

Made with ❤️‍🔥 around the world by the Flint team and contributors.