# unicorn/prefer-string-slice

> Require `slice()` instead of `substr()` and `substring()`

Three methods cut a string, and each treats its arguments differently. The second argument of `substr()` is a length, not an end. `substring()` swaps its arguments if the first is larger and treats a negative number as `0`. `slice()` takes a start and an end, counts negative numbers from the end, and works the same way on arrays.

The rule only looks at the method name, so it reports `substr()` and `substring()` on any object.

`--fix` only renames the method when the result stays the same: `substr()` with at most one argument, and `substring()` with non-negative integer literals in ascending order. Rewrite the other calls by hand.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/prefer-string-slice" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```js
const year = date.substring(0, 4);
const month = date.substr(5, 2); // 2 is a length
```

Not reported:

{/* good */}
```js
const year = date.slice(0, 4);
const month = date.slice(5, 7); // 7 is the end
```
